Solved

Updating a SQL Server table

Posted on 2007-11-27
7
157 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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 …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now