Socarsky
Well-known member
I tried to modify field of FirstName's column with new value, and its successfully done.
But if I click on another cell or use arrow keys up or down then modified data does not update and no exception thrown so this is my first question. Second one is that how can I write codes more useful because as much as it looks in my code that I need to write the same thing for the other columns when possible modifying needs to update. It must be another wise way to update data into the database from a DataGridView.
I mean below:
But if I click on another cell or use arrow keys up or down then modified data does not update and no exception thrown so this is my first question. Second one is that how can I write codes more useful because as much as it looks in my code that I need to write the same thing for the other columns when possible modifying needs to update. It must be another wise way to update data into the database from a DataGridView.
I mean below:
VB.NET:
Dim sqlQuery As String = "UPDATE tblContacts SET FirstName = '" & db_firstname & "' WHERE CONTACTID = " & db_contactID & ""
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click Dim MyIndex As Integer Dim db_contactID As Integer Dim db_firstname As String Dim db_lastname As String Dim db_email As String Dim db_phonenumber As String Dim db_tarih As Date Dim conn As New SqlConnection("Data Source=PC-N39\sqlexpress;Initial Catalog=ADO_PRACTICE;Persist Security Info=True;User ID=sa;Password=sas") conn.Open() MyIndex = DataGridView1.CurrentRow.Index db_contactID = CInt(DataGridView1.Item(0, MyIndex).Value) db_firstname = CStr(DataGridView1.Item(1, MyIndex).Value) db_lastname = CStr(DataGridView1.Item(2, MyIndex).Value) db_email = CStr(DataGridView1.Item(3, MyIndex).Value) db_phonenumber = CStr(DataGridView1.Item(4, MyIndex).Value) db_tarih = CDate(DataGridView1.Item(5, MyIndex).Value) Dim sqlQuery As String = "UPDATE tblContacts SET FirstName = '" & db_firstname & "' WHERE CONTACTID = " & db_contactID & "" Dim sqlCommand As New SqlCommand With sqlCommand .CommandText = sqlQuery .Connection = conn .ExecuteNonQuery() End With conn.Close() End Sub
Last edited: