T
travlintom
Hi. I'm new to VB.net and am used to working with recordsets (much
easier). I have a datagrid that, when the user double clicks on a row,
I am giving them a chance to delete it. I thought this would be simple
but it's taking me forever. I am getting an error when I try to delete
the record. I can't figure out what I'm doing wrong.
The error is "Object reference not set to an instance of an object".
None of the incredibly overpriced books I have seem to be of any help.
Someone's knowledge would be greatly appreciated.
Private Sub dgProjItems_DoubleClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles dgProjItems.DoubleClick
Dim CurItemID As String
CurItemID = dgProjItems.Item(dgProjItems.CurrentRowIndex(), 0)
Dim strConnectionString As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data
Source=C:\Databases\NewEstimate.mdb;"
Dim cn As New OleDbConnection(strConnectionString)
Dim strSQLl As String = "Select * from ProjectsItems Where
ItemID = '" & CurItemID & "'"
Dim objCommand1 As New OleDbCommand(strSQLl, cn)
Dim da As New OleDbDataAdapter(objCommand1)
Dim ds As New DataSet
Try
cn.Open()
Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables("ProjectItems").Rows(0).Delete() 'error here
da.Update(ds, "ProjectItems")
Me.dgProjItems.DataSource = ds.Tables("ProjectItems")
Catch OleDbExceptionErr As OleDb.OleDbException
MessageBox.Show(OleDbExceptionErr.Message, "Access SQL")
End Try
End Sub
easier). I have a datagrid that, when the user double clicks on a row,
I am giving them a chance to delete it. I thought this would be simple
but it's taking me forever. I am getting an error when I try to delete
the record. I can't figure out what I'm doing wrong.
The error is "Object reference not set to an instance of an object".
None of the incredibly overpriced books I have seem to be of any help.
Someone's knowledge would be greatly appreciated.
Private Sub dgProjItems_DoubleClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles dgProjItems.DoubleClick
Dim CurItemID As String
CurItemID = dgProjItems.Item(dgProjItems.CurrentRowIndex(), 0)
Dim strConnectionString As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data
Source=C:\Databases\NewEstimate.mdb;"
Dim cn As New OleDbConnection(strConnectionString)
Dim strSQLl As String = "Select * from ProjectsItems Where
ItemID = '" & CurItemID & "'"
Dim objCommand1 As New OleDbCommand(strSQLl, cn)
Dim da As New OleDbDataAdapter(objCommand1)
Dim ds As New DataSet
Try
cn.Open()
Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables("ProjectItems").Rows(0).Delete() 'error here
da.Update(ds, "ProjectItems")
Me.dgProjItems.DataSource = ds.Tables("ProjectItems")
Catch OleDbExceptionErr As OleDb.OleDbException
MessageBox.Show(OleDbExceptionErr.Message, "Access SQL")
End Try
End Sub