Question Dataset and ExecuteNonQuery - Together

hazee

New member
Joined
Dec 6, 2010
Messages
3
Programming Experience
Beginner
How about the below technique of inserting records, removing the current table from dataset and then again populating dataset to navigate through records in text fields on form.

VB.NET:
Dim cmd As New SqlCommand("insert into tblEmp values(4,'Asad','Accountant','10/10/1988')", cn)
Dim dbread = cmd.ExecuteNonQuery()
MsgBox("Record Inserted")
ds.Tables.Remove("tblEmp")
populateDataset()
populateTextboxes(recordIndex)

Routine to populate Dataset.
VB.NET:
Private Sub populateDataset()
  dap.SelectCommand = New SqlCommand("select * from tblEmp", cn)
  dap.Fill(ds, "tblEmp")
End Sub

Routine to populate text fields on the form:
VB.NET:
Private Sub populateTextboxes(ByVal rowNo As Integer)
        TextBox1.Text = ds.Tables(0).Rows(rowNo).Item(0)
        TextBox2.Text = ds.Tables(0).Rows(rowNo).Item(1)
        TextBox3.Text = ds.Tables(0).Rows(rowNo).Item(2)
        TextBox4.Text = ds.Tables(0).Rows(rowNo).Item(3)
End Sub

One of the navigation button (Next Record):
VB.NET:
Private Sub btnN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnN.Click
        If recordIndex < ds.Tables(0).Rows.Count - 1 Then
            recordIndex = recordIndex + 1
            populateTextboxes(recordIndex)
        End If
End Sub

REM where recordIndex is an integer with a defaultvalue=0
 
Why would you bother, when data binding and TableAdapter.Update/TableAdapter.Fill do it all, more easily and make for better code readability?
 
Back
Top