Problem solve Get help with specific problems with your technologies, process and projects.

What could be causing this problem with my add New and delete methods?

I am facing a problem in my add New and delete methods. My code is attached to show you the way I am doing it. Can you help me please on this?

Private Sub cmdView_Click(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles cmdView.Click
        Dim NR As DataRow = DS.Tables("accounts").NewRow()
        NR("ID") = txtID.Text
        NR("Account_Name") = txtName.Text
        NR("Account_Reference") = txtReference.Text
        NR("SubID") = txtSub.Text
        DA.Update(DS, "Accounts")
    End Sub

    Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles cmdDelete.Click
        Dim NR As DataRow = DS.Tables("accounts").Select("ID = '" &
txtID.Text & "'")(0)
        DA.Update(DS, "accounts")
    End Sub

The Add button code looks like it should work, but the delete button needs some work. Before you can use the dataset, you must fill it with the data from the data adaptor. Try adding something like:

   Dim DS As New DataSet()

before calling the first line in the cmdDelete_Click method. That should work. The first one works because you "fill" the dataset with the new row.

As an aside, where is DS declared? Is there a reason it has larger scope than these button events? Always try to limit the scope of an object to the narrowest possible context. If you don't need DS to hang around and be used after the method exits, declare it in the individual procedures.

Dig Deeper on Topics Archive

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.