CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2006
    Posts
    1

    Errors while adding rows to Database

    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 :

    Code:
    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
    Last edited by HanneSThEGreaT; October 2nd, 2006 at 05:10 AM. Reason: Added Code Tags

  2. #2
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: solve my problem

    Welcome to the Forum

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

    Instead of using Cint use Convert.
    Code:
    Dim value As Integer
    value = Convert.ToInt32(TextBox1.Text)
    Shouldn't Department_ID be an autonumber?

  3. #3
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: solve my problem

    I edited the thread title, I hope it is clearer now.
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  4. #4
    Join Date
    Sep 2006
    Location
    Beyrouth VB.Net 2005
    Posts
    70

    Cool Re: solve my problem

    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..........

  5. #5
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: Errors while adding rows to Database

    Except in a multi-user system, where max+1 is silly unless it is being run as part of the insert statement
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured