Solved

Updating a SQL Server table

Posted on 2007-11-27
7
159 Views
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") = ""

            DS_weld1.WELD.Rows.Add(newrow)

        Next

        If DS_weld1.HasChanges Then

            SDA_weld.Update(DS_weld1.GetChanges())

        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.

Cheers.
0
Comment
Question by:FMabey
  • 5
  • 2
7 Comments
 
LVL 18

Expert Comment

by:vbturbo
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

vbturbo
0
 
LVL 18

Expert Comment

by:vbturbo
ID: 20357629
try have a look here

https://filedb.experts-exchange.com/incoming/ee-stuff/5731-Relations.zip 

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

then do the update made to the tables

vbturbo
0
 
LVL 3

Author Comment

by:FMabey
ID: 20357876
Hi,

thanks for replying, but SDA_weld is a SQL data adapter. Not OleDb.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 18

Accepted Solution

by:
vbturbo earned 350 total points
ID: 20357894
Sorry

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

Expert Comment

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

Author Comment

by:FMabey
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")

endif

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?
0
 
LVL 18

Expert Comment

by:vbturbo
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.


http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqlcommandbuilder.aspx

vbturbo
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access to class from any project within a solution. 6 28
VB.NET 1 27
Convert datetime to time string 10 28
Create a datatable in vb.net dynamically 1 22
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

766 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