Updating a SQL Server table

Posted on 2007-11-27
Last Modified: 2010-04-23
Hi all,

I am using the following code to transfer items from one datagridview to another and then update the sql server table with the new data.

    Private Sub BTN_stf_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN_stf.Click

            Dim STR_part As String = ""
            Dim STR_gdr As String = ""
            Dim STR_type As String = ""

        'go through the selected rows
        For Each dgvr As DataGridViewRow In DGV_gdrstfs.SelectedRows

            STR_part = dgvr.Cells("PRT_MARK").Value
            STR_gdr = dgvr.Cells("PRT_GDR_NO").Value
            STR_type = dgvr.Cells("PRT_PART_TYPE1").Value

            Dim newrow As DataRow = DS_weld1.WELD.NewRow
            newrow("WLD_SORT") = dgvr.Cells("PRT_ITEM_KEY").Value
            newrow("WLD_PART_ID") = STR_part
            newrow("WLD_GDR_NO") = STR_gdr
            newrow("WLD_CONT") = PUB_Cont
            newrow("WLD_STR") = PUB_Str
            newrow("WLD_THICKNESS") = dgvr.Cells("PRT_THICKNESS").Value
            newrow("WLD_PART_TYPE") = dgvr.Cells("PRT_PART_TYPE1").Value
            newrow("WLD_FACE") = ""
            newrow("WLD_END") = ""



        If DS_weld1.HasChanges Then


        End If

    End Sub

My problem is that. When I move the 1st item over (say item no P10), I see one new record in my SQL server table. However, when I now move the 2nd item (say P15), I now see two P10's. But why is this? I can still only see one P10 in my datagridview.

Any help would be great.

Question by:FMabey
  • 5
  • 2
LVL 18

Expert Comment

ID: 20357561
Try use the commandbuilder object

        If DS_weld1.HasChanges Then

        Dim cb As New OleDb.OleDbCommandBuilder(SDA_weld)
        SDA_weld.Update(DS_weld1, "MyTableName")
        End If

LVL 18

Expert Comment

ID: 20357629
try have a look here 

type some data in the one of the grids or create/delete a  row(s)

then do the update made to the tables


Author Comment

ID: 20357876

thanks for replying, but SDA_weld is a SQL data adapter. Not OleDb.
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

LVL 18

Accepted Solution

vbturbo earned 350 total points
ID: 20357894

Dim cb As New SqlClient.SqlCommandBuilder(SDA_weld)
LVL 18

Expert Comment

ID: 20357908
For the adapter to perform update and delete commands your table has to be setup with a primary key

Author Comment

ID: 20357984
Yep, it has a primary key... It's an identity field.

Ok, this works:

If DS_weld.haschanges() then

SDA_weld.Update(DS_weld1, "WELD")


But why doesn't my previous code work properly? I use it in numerous other places at it works fine. Which is the best method to use?
LVL 18

Expert Comment

ID: 20358061
The SqlDataAdapter does not automatically generate the Transact-SQL statements required to reconcile changes made to a DataSet with the associated instance of SQL Server. However, you can create a SqlCommandBuilder object to automatically generate Transact-SQL statements for single-table updates if you set the SelectCommand property of the SqlDataAdapter. Then, any additional Transact-SQL statements that you do not set are generated by the SqlCommandBuilder.


Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
LINQ - C# to VB convertion 12 58
Variable Event ? 3 28
Get distinct values from excel or comm seperated file 4 25 Progress Bar - Maximum Value too large 2 12
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question