Question problem in updating

nitish_07

Member
Joined
Feb 14, 2012
Messages
7
Programming Experience
Beginner
I have a gridview. I want to insert or update in table through datagridview....
rowleave event coding is given below-


Private Sub dataGridView1_RowLeave(sender As Object, e As DataGridViewCellEventArgs) Try If dataGridView1.CurrentRow.Cells(0).Value.ToString() IsNot Nothing Then For j As Integer = 0 To i - 1 s = "'" + dataGridView1.CurrentRow.Cells(j).Value & "'" & "," s1 = s1 + s Next s1 = s1.Substring(0, s1.Length - 1) Try If dataGridView1.Rows.Count > m Then sql = ("insert into" & " ") + tablenametext.Text & " " & "values" & "(" & s1 & ")" & ";" m += 1 Dim cmd As New OdbcCommand(sql, c.cn) cmd.CommandText = sql cmd.ExecuteNonQuery() Else 'if (dataGridView1.CurrentRow.Cells[0].Value !=null) If t1 <> " " Then sql = (("update" & " ") + tablenametext.Text & " " & "set" & " " & t1 & "where" & " " & p & "=" & "'") + dataGridView1.CurrentRow.Cells(ind).Value & "'" & ";" Dim cmd As New OdbcCommand(sql, c.cn) cmd.CommandText = sql cmd.ExecuteNonQuery() ' else 'MessageBox.Show("row is empty so fill values first"); End If End If Catch generatedExceptionName As Exception End Try s1 = "" t1 = "" End If Catch ee As Exception MessageBox.Show(ee.Message) End Try ' bind(); End Sub
and on cell state changed given below-


Private Sub dataGridView1_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) For j As Integer = 0 To i - 1 t = dataGridView1.Columns(j).Name + "=" & "'" & dataGridView1.CurrentRow.Cells(j).Value.ToString() & "'" & "," t1 = t1 + t Next t1 = t1.Substring(0, t1.Length - 1) End Sub


There are two problems. Updating is not done through this and it gets null value for the last columns value of datagridview.....so plz try to resolve it asap....thanks....
 
Even though I knew what I was looking for, it still took me minutes to find it in that mess - PLEASE use CODE tags in the future to make it legible!

I suggest you stop, and look at any of the links on this forum relating to Data Applications and Worked examples - if you can, find posts by user "cjard" and follow the links in his signature. You are making life so difficult for yourself by trying to write it in that manner.

However, to answer your specific question, perhaps inserting a space after the table name t1 before the "where" command might help.
 
Back
Top