V
Viravalo
Guest
Hey guys, so I'm trying to update some data from a datagridview to my SQL database. Once I make changes, it affects all rows except the first one. I'll show you the code:
Private Sub ShowData()
connection = New SqlConnection(svrcon)
sql = $"SELECT * FROM Aguinaldo WHERE NombreAguinaldo = '{NombreTxt.Text}' AND FechaGenerada = '{fecha}'"
Try
cn.Open()
adapter = New SqlDataAdapter(sql, cn)
adapter.Fill(ds)
DataGridView1.DataSource = ds.Tables(0)
cn.Close()
Catch ex As Exception
MessageBox.Show($"Error: {ex.ToString}", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
*This is how I show my data in the datagrid view. Connection string is declared as public.
Private Sub updateDG()
Try
cmdBuilder = New SqlCommandBuilder(adapter)
changes = ds.GetChanges()
If changes IsNot Nothing Then
adapter.Update(changes)
ds.AcceptChanges()
DataGridView1.AllowUserToOrderColumns = False
End If
MessageBox.Show("¡DATOS DE AGUINALDO GUARDADOS EXITOSAMENTE!", "DATOS GUARDADOS", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show($"Error: {ex.ToString}", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
*And this is my update method, as I said previously, it updates every row except the first one.
When I want to update the first row manually, it gives me this error:
Please I need your help!
Continue reading...
Private Sub ShowData()
connection = New SqlConnection(svrcon)
sql = $"SELECT * FROM Aguinaldo WHERE NombreAguinaldo = '{NombreTxt.Text}' AND FechaGenerada = '{fecha}'"
Try
cn.Open()
adapter = New SqlDataAdapter(sql, cn)
adapter.Fill(ds)
DataGridView1.DataSource = ds.Tables(0)
cn.Close()
Catch ex As Exception
MessageBox.Show($"Error: {ex.ToString}", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
*This is how I show my data in the datagrid view. Connection string is declared as public.
Private Sub updateDG()
Try
cmdBuilder = New SqlCommandBuilder(adapter)
changes = ds.GetChanges()
If changes IsNot Nothing Then
adapter.Update(changes)
ds.AcceptChanges()
DataGridView1.AllowUserToOrderColumns = False
End If
MessageBox.Show("¡DATOS DE AGUINALDO GUARDADOS EXITOSAMENTE!", "DATOS GUARDADOS", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show($"Error: {ex.ToString}", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
*And this is my update method, as I said previously, it updates every row except the first one.
When I want to update the first row manually, it gives me this error:
Please I need your help!
Continue reading...