Socarsky
Well-known member
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:
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: