Question Inserting and updating together

nitish_07

Member
Joined
Feb 14, 2012
Messages
7
Programming Experience
Beginner
I have a datagridview with selected tables column..and i want to insert data in to that table or user can also update their data through gridview..I have an apply button outside gridview..Coding on apply button is given below.-


Try For Each gr As DataGridViewRow In dataGridView1.Rows If gr.Cells(0).Value IsNot Nothing Then For j As Integer = 0 To i - 1 s = "'" & gr.Cells(j).Value.ToString() & "'" & "," s1 = s1 + s t = dataGridView1.Columns(j).Name + "=" & "'" & gr.Cells(j).Value.ToString() & "'" & "," t1 = t1 + t Next s1 = s1.Substring(0, s1.Length - 1) t1 = t1.Substring(0, t1.Length - 1) Try If gr.Index = dataGridView1.Rows.Count - 2 Then If u = 1 Then sql = ("insert into" & " ") + tablenametext.Text & " " & "values" & "(" & s1 & ")" & ";" u = 0 Else sql = (("update" & " ") + tablenametext.Text & " " & "set" & " " & t1 & " " & "where" & " " & p & "=" & "'") + gr.Cells(ind).Value & "'" & ";" End If Else sql = (("update" & " ") + tablenametext.Text & " " & "set" & " " & t1 & " " & "where" & " " & p & "=" & "'") + gr.Cells(ind).Value & "'" & ";" End If Catch generatedExceptionName As Exception End Try Dim cmd As New OdbcCommand(sql, c.cn) cmd.CommandText = sql cmd.ExecuteNonQuery() s1 = "" t1 = "" End If Next Catch ee As Exception MessageBox.Show(ee.Message) End Try bind()
and on cell_click event is given below.-

If dataGridView1.CurrentRow.Index = dataGridView1.Rows.Count - 1 Then
If dataGridView1.CurrentRow.Cells(0).Value.ToString() = "" Then
u = 1
Else
u = 0
End If
End If






This is working fine if user enter one row and click on apply..But if user enters 2 rows at a time then click on apply then it is not working...and one more thing when i update the primary key columns value it does not update it because it takes new values according to my coding but i want the old one...

So there are two big problems...Plz sort it out asap....thanks....:)
 
Last edited:

Latest posts

Back
Top