Click to See Complete Forum and Search --> : Errors while adding rows to Database


SabaRehan
September 28th, 2006, 02:49 AM
i am working on two databases one is for human resource management and the other is of projects details. my problem is occuring while saving record or deleting record. i've created a form of departments where i am inserting Department_ID, Department_Name and Location when i entered values and press add then there is an erroe of type casting.
i am casting the text values into integer but the problem is still unsolved the code behind add button is :

Dim drAdd As DataRow
Dim value As Integer
value = CInt(TextBox1.Text)
drAdd = DsDep1.Tables("Departments_Detail").NewRow()
drAdd(0) = value
drAdd(1) = TextBox2.Text
drAdd(2) = TextBox3.Text
DsDep1.Tables("Departments_Detail").Rows.Add(drAdd)
DaDep.Update(DsDep1, "Departments_Detail")

MsgBox("New Record added to the Database")

data types are:

Department_ID = Integer
Department_Name = Varchar
Location = Text

Shuja Ali
September 28th, 2006, 02:56 AM
Welcome to the Forum :wave:

"Solve my Problem" is not a good thread title. Use descriptive thread titles.

Instead of using Cint use Convert. Dim value As Integer
value = Convert.ToInt32(TextBox1.Text)

Shouldn't Department_ID be an autonumber?

NoHero
September 28th, 2006, 11:17 AM
I edited the thread title, I hope it is clearer now.

Nagham
September 29th, 2006, 01:51 AM
Hi there,first you can use value=val(TextBox1.Text)
and since Department-ID is not autonumber,and it's preferable,try to bring always max(department_ID) by a query and then add to it +1,so u have to let your TextBox empty and disable,befor the save bring the max(....) addto it 1 and put it in the textbox..........

cjard
October 5th, 2006, 08:11 AM
Except in a multi-user system, where max+1 is silly unless it is being run as part of the insert statement