Results 1 to 5 of 5

Thread: [RESOLVED] Error with add new row to table VBE2005-SQL server

  1. #1

    Thread Starter
    Hyperactive Member wizkid's Avatar
    Join Date
    Dec 2005
    Location
    uk
    Posts
    405

    Resolved [RESOLVED] Error with add new row to table VBE2005-SQL server

    I am trying to add new row to the table
    but i can't
    error is object reference not set to an instance of an object

    code:
    VB Code:
    1. Dim cust_det_ds As New DataSet
    2.   Dim cust_det_da As New OleDb.OleDbDataAdapter
    3.   If count_custdetails2 = 0 Then 'if the customer is not in cust_details2 table
    4.       Dim cb1 As New OleDb.OleDbCommandBuilder(cust_det_da)
    5.    Dim dsNewRow As DataRow
    6.       dsNewRow = cust_det_ds.Tables("cust_details2").NewRow()
    7.       dsNewRow.Item("Account_no") = txtCustNo.Text
    8.  dsNewRow.Item("Title") = txtTitle.Text
    9.     dsNewRow.Item("Initial") = txtInitial.Text
    10.     dsNewRow.Item("Surname") = txtSurname.Text
    11.       dsNewRow.Item("HouseName") = txtHname.Text
    12.      dsNewRow.Item("Address1") = txtAdd1.Text
    13.        dsNewRow.Item("Address2") = txtAdd2.Text
    14.             dsNewRow.Item("Address3") = txtAdd3.Text
    15.               dsNewRow.Item("Address4") = txtAdd4.Text
    16.                  dsNewRow.Item("Address5") = txtAdd5.Text
    17.        dsNewRow.Item("Postcode") = txtPostcode.Text
    18.        cust_det_ds.Tables("cust_details2").Rows.Add(dsNewRow)
    19.     cust_det_da.Update(cust_det_ds, "cust_details2")
    20.  Else
    21.   End If

    cust_det_ds.Tables("cust_details2").NewRow() error is in this line
    Any idea?
    thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Error with add new row to table VBE2005-SQL server

    You're trying to add a row to a table that doesn't exist. You create the DataSet five lines before that and nowhere in between do you add any tables to it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member wizkid's Avatar
    Join Date
    Dec 2005
    Location
    uk
    Posts
    405

    Re: Error with add new row to table VBE2005-SQL server

    here cust_details is the table
    cust_det_ds.Tables("cust_details2").Rows.Add(dsNewRow)

    i think the above line will add the new??
    is it a wrong one?
    thanks

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Error with add new row to table VBE2005-SQL server

    I've already told you what the problem is. You've never created a DataTable with that name. In fact, you've never created any DataTables at all. Put this just before the line that is throwing the exception:
    VB Code:
    1. If cust_det_ds.Tables.Count = 0 Then
    2.     MessageBox.Show("I told you so!  There are no tables at all.")
    3. Else
    4.     MessageBox.Show("Well, I was wrong after all.")
    5. End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member wizkid's Avatar
    Join Date
    Dec 2005
    Location
    uk
    Posts
    405

    Re: Error with add new row to table VBE2005-SQL server

    Thanks
    I find the mistake
    I missed the code in red
    Dim sql As String

    sql = "select * from Cust_Details2"
    cust_det_da = New OleDb.OleDbDataAdapter(sql, objconnection)
    cust_det_da.Fill(cust_det_ds, "cust_details2")



    This is the new code

    VB Code:
    1. Dim cust_det_ds As New DataSet
    2.                                                                 Dim cust_det_da As OleDb.OleDbDataAdapter
    3.  
    4.                                                                 Dim sql As String
    5.  
    6.                                                                 sql = "select * from Cust_Details2"
    7.                                                                 cust_det_da = New OleDb.OleDbDataAdapter(sql, objconnection)
    8.                                                                 cust_det_da.Fill(cust_det_ds, "cust_details2")
    9.  
    10.                                                                 'If cust_det_ds.Tables.Count = 0 Then
    11.                                                                 '    MessageBox.Show("I told you so!  There are no tables at all.")
    12.                                                                 'Else
    13.                                                                 '    MessageBox.Show("Well, I was wrong after all.")
    14.                                                                 'End If
    15.                                                                 If count_custdetails2 = 0 Then
    16.                                                                     Dim cb1 As New OleDb.OleDbCommandBuilder(cust_det_da)
    17.                                                                     Dim dsNewRow As DataRow
    18.                                                                     dsNewRow = cust_det_ds.Tables("Cust_Details2").NewRow()
    19.                                                                     ' Row1 = "Cust_details2".NewRow()
    20.                                                                     dsNewRow.Item("Account_no") = txtCustNo.Text
    21.                                                                     dsNewRow.Item("Title") = txtTitle.Text
    22.                                                                     dsNewRow.Item("Initial") = txtInitial.Text
    23.                                                                     dsNewRow.Item("Surname") = txtSurname.Text
    24.                                                                     dsNewRow.Item("HouseName") = txtHname.Text
    25.                                                                     dsNewRow.Item("Address1") = txtAdd1.Text
    26.                                                                     dsNewRow.Item("Address2") = txtAdd2.Text
    27.                                                                     dsNewRow.Item("Address3") = txtAdd3.Text
    28.                                                                     dsNewRow.Item("Address4") = txtAdd4.Text
    29.                                                                     dsNewRow.Item("Address5") = txtAdd5.Text
    30.                                                                     dsNewRow.Item("Postcode") = txtPostcode.Text
    31.  
    32.                                                                     cust_det_ds.Tables("cust_details2").Rows.Add(dsNewRow)
    33.                                                                     cust_det_da.Update(cust_det_ds, "cust_details2")
    34.                                                                 Else
    35.                                                                    
    36.                                                                 End If

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