I'm using a form with a datagrid to edit tables directly from the database, which I'm calling from my main form. I show this form by using MyForm.ShowDialog(). I use the following code to update the database every time the row is changed in my datagrid.
Almost everything runs fine. When I try to delete the last row in the datagrid, I get
This error doesn't occur in the form with the datagrid, it occurs in my main form which called this form. When I step through the code, the above code completes fine, and then the app goes directly to my main form and gives me this error. Been rackin' my brain all day on this one. Any ideas? Thanks.
Code:
Private Sub datagrid1_currentcellchanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.CurrentCellChanged
Dim tempdataset As DataSet
Dim tempcell As DataGridCell
If Not DataGrid1.CurrentRowIndex = previousCell.RowNumber Then
'create a new dataset to hold the changes that have been made to the main dataset.
Dim datasetchanges As DataSet = New DataSet
'me.bindingcontext(dataset1, databasename).cancelcurrentedit()
'get the changes that have been made to the main dataset
datasetchanges = DataSet1.GetChanges
'check to see if any changes have been made
If (Not (datasetchanges) Is Nothing) Then
'there are changes that need to be made, so attempt to update the datasource by
' calling the update method and passing the dataset
Try
'open the connection.
SqlConnection1.Open()
tempdataset = DataSet1
'attempt to update the data source.
dataAdapter.Update(datasetchanges)
Catch updateexception As System.Exception
MsgBox("problem saving one or more entries. it is most" & vbNewLine & _
"likely that some of your data violates foreign" & vbNewLine & _
"key constraints. if problem persists please" & vbNewLine & _
"contact your friendly it specialist.", MsgBoxStyle.Exclamation, "error saving data")
DataGrid1.CurrentCell = previousCell
For counter As Integer = 0 To previousRow.ItemArray.GetLength(0) - 1
Try
DataSet1.Tables(databaseName).Rows(DataGrid1.CurrentRowIndex).Item(counter) = previousRow.Item(counter)
Catch ex As ReadOnlyException
'MsgBox("readonlyexception thrown")
End Try
Next
dataAdapter.Fill(DataSet1)
End Try
DataSet1.AcceptChanges()
Try
SqlConnection1.Close()
Catch connexception As Exception
MsgBox("problem closing sql connection." & vbNewLine & vbNewLine & "details: " & _
connexception.Message)
End Try
End If
previousCell = DataGrid1.CurrentCell
Try
previousRow = DataSet1.Tables(databaseName).Rows(DataGrid1.CurrentRowIndex)
Catch iOOREx As IndexOutOfRangeException
MsgBox("IndexOutOfRangeException Thrown")
End Try
End If
End Sub
Code:
A first chance exception of type 'System.NullReferenceException' occurred in system.data.dll
Additional information: Object reference not set to an instance of an object.