Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 181
  • Last Modified:

Dataset Update Question

i have a datagrid populated by a dataset called: "DS_Vendor1" - showing data from a table called "Vendor" - I recently asked a question here about updating and a helpful member, after giving me the perfect answer - suggested that it would be easier to work directly with the dataset (he was right) - in any case - here is my question: I am currently using the below code to add a new entry - it's working exactly as I want it to - what i'd like to be able to do is have 2 more similar routines that work just like the code below, but one needs UPDATE functionality and the other DELETE functionality

            '''what would this command be to do an update? i need to be able to pass it the id(primary key, autonumber) of the row i want to update
            '''same for delete - need to be able to give it the id(primary key, autonumber) of the record to delete
            Dim NewRow As DataRow = DS_Vendor1.Vendor.NewRow

            '''and on UPDATE set all the values for the row with that id to the below text field values
            '''similar to what i'm doing below for adding new values
            NewRow.Item("Vendor_Name") = Trim(v_name.Text)
            NewRow.Item("Vendor_Addr1") = Trim(v_addr1.Text)
            NewRow.Item("Vendor_Addr2") = Trim(v_addr2.Text)
            NewRow.Item("Vendor_City") = Trim(v_city.Text)
            NewRow.Item("Vendor_State") = Trim(v_state.Text)
            NewRow.Item("Vendor_Zip") = Trim(v_zip.Text)
            NewRow.Item("Vendor_Phone") = Trim(v_phone.Text)
            NewRow.Item("Vendor_Fax") = Trim(v_fax.Text)
            NewRow.Item("Vendor_Contact") = Trim(v_contact.Text)
            NewRow.Item("Vendor_Email") = Trim(v_email.Text)
            NewRow.Item("Vendor_Website") = Trim(v_website.Text)

            '''Then the code that actually executes the UPDATE/DELETE...just as this one does for ADD
            DS_Vendor1.Vendor.Rows.Add(NewRow)

sorry to be asking such a basic question, but there are just TOO many ways to do the same thing in vb.net : - ) - i really like the way the above code works (found the example right here on EE), and im' thinking there has to be a very simple one-liner where u pass the id of the record u want updated and then assign the values....and same for delete, but time is running short and i haven't found it yet -
0
friskyweasel
Asked:
friskyweasel
1 Solution
 
amyhxuCommented:
To update:
'Suppose you have a textbox called txtID where user can enter ID. (Maybe you use a comboboxto list the ID) just get the primary key value to Find() method
Dim dRow As DataRow = DS_Vendor1.Vendor.Rows.Find(Convert.ToInt32(txtID.Text))  'find the row currently displayed
dRow("Vendor_Name") = v_name.Text.Trim
dRow("Vendor_Addr1") = v_addr1.Text.Trim
...


To Delete:
'Use the same code as in update method to find the current row
Dim dRow As DataRow = DS_Vendor1.Vendor.Rows.Find(Convert.ToInt32(txtID.Text))
dRow.Delete()

When finally it's time to update the database, use
myDataAdapter.Update(DS_Vendor1.Vendor)
0
 
friskyweaselAuthor Commented:
Your suggestion worked perfectly. Thanks a lot!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now