Question not to duplicate data rows

Socarsky

Well-known member
Joined
Dec 27, 2012
Messages
173
Location
Jakarta/Indonesia
Programming Experience
Beginner
I can able to get a row of fields data from a DataGridView to input into the Database but after inserting the data to the Database
then how I can control the button when it hits more then one time as update but after once time then the code product duplicate data.
What should I do to fix that?

So here are the lines of the code:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim FirstName As String = ""
Dim LastName As String = ""
Dim Email As String = ""
Dim PhoneNumber As String = ""
Dim Tarih As String = ""
Dim ConnectionString As String = "Data Source=PC-N39\sqlexpress;Initial Catalog=ADO_PRACTICE;Persist Security Info=True;User ID=sa;Password=sas"
Const strInsert As String = "Insert Into tblContacts(FirstName,LastName,Email,PhoneNumber,T arih) values (@CN,@CD,@PN,@D1,@DA)"
Dim con1 As New SqlConnection(ConnectionString)
Dim cmdInsert As New SqlCommand(strInsert, con1)

con1.Open()

Dim parameters = cmdInsert.Parameters

Dim p1 = parameters.Add("@CN", SqlDbType.VarChar, 50, FirstName)
Dim p2 = parameters.Add("@CD", SqlDbType.VarChar, 50, LastName)
Dim p3 = parameters.Add("@PN", SqlDbType.VarChar, 50, Email)
Dim p4 = parameters.Add("@D1", SqlDbType.VarChar, 50, PhoneNumber)
Dim p5 = parameters.Add("@DA", SqlDbType.VarChar, 50, Tarih)


For Each DGR As DataGridViewRow In DataGridView2.Rows
If Not DGR.IsNewRow Then
FirstName = DGR.Cells(0).Value.ToString()
LastName = DGR.Cells(1).Value.ToString()
Email = DGR.Cells(2).Value.ToString()
PhoneNumber = DGR.Cells(3).Value.ToString()
Tarih = DGR.Cells(4).Value.ToString()

p1.Value = FirstName
p2.Value = LastName
p3.Value = Email
p4.Value = PhoneNumber
p5.Value = Tarih

cmdInsert.ExecuteNonQuery()
End If
Next
con1.Close()
End Sub
 
Last edited:
People just love making life hard for themselves. Don't use ExecuteNonQuery in a loop to save the data in the first place. Create a DataTable and bind it to the grid and then use a data adapter to save it. If you do that then your current issue simply goes away. You can call Update on the data adapter as many times as you like and no data will be inserted more than once.
 
People just love making life hard for themselves. Don't use ExecuteNonQuery in a loop to save the data in the first place. Create a DataTable and bind it to the grid and then use a data adapter to save it. If you do that then your current issue simply goes away. You can call Update on the data adapter as many times as you like and no data will be inserted more than once.
I actually tried to find a topic which has written this forum and you answered just like I copied to mine :) it works for that but now I am seeing that its something wrong in that code. Let me try it with your update just you mentioned or if I see me in a difficulty then I will try to do something with your another topic which is in vbforums.com.
 
By using a DataTable, every DataRow has a RowState property. If you retrieve any data from the database into the DataTable using the data adapter then that RowState will be Unchanged for those rows. If you add any new rows, their RowState will be Added. When you save the data, only the rows with a RowState of Added will be inserted. After a successful save, AcceptChanges is automatically called on the DataTable and every RowState is set to Unchanged. You can then call Update again and again and all those rows will simply be ignored.
 
Socarsky, to mark your thread as resolved, edit the first post, click the go advanced button, and change the Prefix.
 
There is a minimum number of posts required to be able to edit but I would think that you've made that by now. Do you not see this when you log in: VB.NET Forums.PNG
 
Back
Top