am writing a VB.NET program to update (add & save) database in runtime.The database (MS SQL Server 7) is “customertracking” with the following fields: custid(primary key), fname, lname, address,phone,email
My form design has the following:
6 Labels (Customer ID, First Name, Last Name, Address, Phone, Email)
6 Textboxes for the corresponding labels
4 Buttons (ViewFirstRecord, ViewNextRecord, ViewPreviousRecord, ViewLastRecord)
2 more Buttons (Add, Save)
I am getting the error, “Update requires a valid InsertCommand when passed DataRow collection with new rows.” under Private sub Save_Click…
Please debug.
Private Sub Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Add.Click
TextBox2.Text = " "
TextBox3.Text = " "
TextBox4.Text = " "
TextBox5.Text = " "
TextBox6.Text = " "
flag = 1
Dim ctr, len As Integer
Dim cid, cidval As String
dt = ds.Tables("customertracking")
len = (dt.Rows.Count - 1)
dr = dt.Rows(len)
cid = dr("custid")
cidval = Mid(cid, 2, 3)
ctr = CInt(cidval)
If ctr >= 1 And ctr < 9 Then
ctr = ctr + 1
TextBox1.Text = "C00" & ctr
ElseIf ctr >= 9 And ctr < 99 Then
ctr = ctr + 1
TextBox1.Text = "C0" & ctr
ElseIf ctr >= 99 Then
ctr = ctr + 1
TextBox1.Text = "C" & ctr
End If
TextBox1.Enabled = False
End Sub
Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click
If flag = 1 Then
dt = ds.Tables("customertracking")
dr = dt.NewRow()
dr(0) = TextBox1.Text
dr(1) = TextBox2.Text
dr(2) = TextBox3.Text
dr(3) = TextBox4.Text
dr(4) = TextBox5.Text
dr(5) = TextBox6.Text
dt.Rows.Add(dr)
TextBox1.Enabled = True
flag = 0
Dim adapter As New OleDb.OleDbDataAdapter("select * from customertracking", cn)
adapter.Update(ds, "customertracking")
adapter.Fill(ds)
bm.Position = 0
End Sub
End Class
The above program works fine for updating (saving the records) database in design time. Please give me the corresponding code to update database in runtime?
My form design has the following:
6 Labels (Customer ID, First Name, Last Name, Address, Phone, Email)
6 Textboxes for the corresponding labels
4 Buttons (ViewFirstRecord, ViewNextRecord, ViewPreviousRecord, ViewLastRecord)
2 more Buttons (Add, Save)
I am getting the error, “Update requires a valid InsertCommand when passed DataRow collection with new rows.” under Private sub Save_Click…
Please debug.
Private Sub Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Add.Click
TextBox2.Text = " "
TextBox3.Text = " "
TextBox4.Text = " "
TextBox5.Text = " "
TextBox6.Text = " "
flag = 1
Dim ctr, len As Integer
Dim cid, cidval As String
dt = ds.Tables("customertracking")
len = (dt.Rows.Count - 1)
dr = dt.Rows(len)
cid = dr("custid")
cidval = Mid(cid, 2, 3)
ctr = CInt(cidval)
If ctr >= 1 And ctr < 9 Then
ctr = ctr + 1
TextBox1.Text = "C00" & ctr
ElseIf ctr >= 9 And ctr < 99 Then
ctr = ctr + 1
TextBox1.Text = "C0" & ctr
ElseIf ctr >= 99 Then
ctr = ctr + 1
TextBox1.Text = "C" & ctr
End If
TextBox1.Enabled = False
End Sub
Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click
If flag = 1 Then
dt = ds.Tables("customertracking")
dr = dt.NewRow()
dr(0) = TextBox1.Text
dr(1) = TextBox2.Text
dr(2) = TextBox3.Text
dr(3) = TextBox4.Text
dr(4) = TextBox5.Text
dr(5) = TextBox6.Text
dt.Rows.Add(dr)
TextBox1.Enabled = True
flag = 0
Dim adapter As New OleDb.OleDbDataAdapter("select * from customertracking", cn)
adapter.Update(ds, "customertracking")
adapter.Fill(ds)
bm.Position = 0
End Sub
End Class
The above program works fine for updating (saving the records) database in design time. Please give me the corresponding code to update database in runtime?