|
-
Jul 5th, 2006, 03:52 AM
#1
Thread Starter
Hyperactive Member
[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:
Dim cust_det_ds As New DataSet
Dim cust_det_da As New OleDb.OleDbDataAdapter
If count_custdetails2 = 0 Then 'if the customer is not in cust_details2 table
Dim cb1 As New OleDb.OleDbCommandBuilder(cust_det_da)
Dim dsNewRow As DataRow
dsNewRow = cust_det_ds.Tables("cust_details2").NewRow()
dsNewRow.Item("Account_no") = txtCustNo.Text
dsNewRow.Item("Title") = txtTitle.Text
dsNewRow.Item("Initial") = txtInitial.Text
dsNewRow.Item("Surname") = txtSurname.Text
dsNewRow.Item("HouseName") = txtHname.Text
dsNewRow.Item("Address1") = txtAdd1.Text
dsNewRow.Item("Address2") = txtAdd2.Text
dsNewRow.Item("Address3") = txtAdd3.Text
dsNewRow.Item("Address4") = txtAdd4.Text
dsNewRow.Item("Address5") = txtAdd5.Text
dsNewRow.Item("Postcode") = txtPostcode.Text
cust_det_ds.Tables("cust_details2").Rows.Add(dsNewRow)
cust_det_da.Update(cust_det_ds, "cust_details2")
Else
End If
cust_det_ds.Tables("cust_details2").NewRow() error is in this line
Any idea?
thanks
-
Jul 5th, 2006, 03:56 AM
#2
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.
-
Jul 5th, 2006, 04:00 AM
#3
Thread Starter
Hyperactive Member
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
-
Jul 5th, 2006, 04:35 AM
#4
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:
If cust_det_ds.Tables.Count = 0 Then
MessageBox.Show("I told you so! There are no tables at all.")
Else
MessageBox.Show("Well, I was wrong after all.")
End If
-
Jul 5th, 2006, 04:55 AM
#5
Thread Starter
Hyperactive Member
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:
Dim cust_det_ds As New DataSet
Dim cust_det_da As OleDb.OleDbDataAdapter
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")
'If cust_det_ds.Tables.Count = 0 Then
' MessageBox.Show("I told you so! There are no tables at all.")
'Else
' MessageBox.Show("Well, I was wrong after all.")
'End If
If count_custdetails2 = 0 Then
Dim cb1 As New OleDb.OleDbCommandBuilder(cust_det_da)
Dim dsNewRow As DataRow
dsNewRow = cust_det_ds.Tables("Cust_Details2").NewRow()
' Row1 = "Cust_details2".NewRow()
dsNewRow.Item("Account_no") = txtCustNo.Text
dsNewRow.Item("Title") = txtTitle.Text
dsNewRow.Item("Initial") = txtInitial.Text
dsNewRow.Item("Surname") = txtSurname.Text
dsNewRow.Item("HouseName") = txtHname.Text
dsNewRow.Item("Address1") = txtAdd1.Text
dsNewRow.Item("Address2") = txtAdd2.Text
dsNewRow.Item("Address3") = txtAdd3.Text
dsNewRow.Item("Address4") = txtAdd4.Text
dsNewRow.Item("Address5") = txtAdd5.Text
dsNewRow.Item("Postcode") = txtPostcode.Text
cust_det_ds.Tables("cust_details2").Rows.Add(dsNewRow)
cust_det_da.Update(cust_det_ds, "cust_details2")
Else
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|