M
Mahatma
Hey, i am trying to delete a record from an SQL database
table named CourseList.
The code i am using is the same code that has worked for
me before. When i run it i get the following error:
An unhandled exception of
type 'System.NullReferenceException' occurred in QPCU
Database frontend.exe
Additional information: Object reference not set to an
instance of an object.
Tells me that oDR = Nothing
Here is the code.
Private Sub DataCourseDelete()
Dim oAdapter As SqlClient.SqlDataAdapter
Dim oBuild As SqlClient.SqlCommandBuilder
Dim oDR As DataRow
Dim strSQL As String
Dim strID As String
Dim strConn As String
' Get Connection String
strConn = ConnectStringBuild()
' Get Primary Key From List Box
strID = CType(lstCourse.SelectedItem,
PDSAListItemNumeric).ID.ToString()
' Find DataRow To Delete
oDR = moDS.Tables("CourseList").Rows.Find(CInt
(strID))
' Mark DataRow for deletion
oDR.Delete()
Try
' Build SQL String
strSQL = "SELECT * FROM CourseList "
' Create New DataAdapter
oAdapter = New _
SqlClient.SqlDataAdapter(strSQL, strConn)
' Create CommandBuild from Adapter
' This will build INSERT, UPDATE and DELETE
SQL
oBuild = New SqlClient.SqlCommandBuilder
(oAdapter)
' Get Delete Command Object
oAdapter.DeleteCommand =
oBuild.GetDeleteCommand()
' Submit DELETE through Adapter
oAdapter.Update(moDS, "CourseList")
' Tell DataSet changes to data source are
complete
moDS.AcceptChanges()
' Reload the list box
CourseListLoad()
Catch oException As Exception
MessageBox.Show(oException.Message)
End Try
End Sub
table named CourseList.
The code i am using is the same code that has worked for
me before. When i run it i get the following error:
An unhandled exception of
type 'System.NullReferenceException' occurred in QPCU
Database frontend.exe
Additional information: Object reference not set to an
instance of an object.
Tells me that oDR = Nothing
Here is the code.
Private Sub DataCourseDelete()
Dim oAdapter As SqlClient.SqlDataAdapter
Dim oBuild As SqlClient.SqlCommandBuilder
Dim oDR As DataRow
Dim strSQL As String
Dim strID As String
Dim strConn As String
' Get Connection String
strConn = ConnectStringBuild()
' Get Primary Key From List Box
strID = CType(lstCourse.SelectedItem,
PDSAListItemNumeric).ID.ToString()
' Find DataRow To Delete
oDR = moDS.Tables("CourseList").Rows.Find(CInt
(strID))
' Mark DataRow for deletion
oDR.Delete()
Try
' Build SQL String
strSQL = "SELECT * FROM CourseList "
' Create New DataAdapter
oAdapter = New _
SqlClient.SqlDataAdapter(strSQL, strConn)
' Create CommandBuild from Adapter
' This will build INSERT, UPDATE and DELETE
SQL
oBuild = New SqlClient.SqlCommandBuilder
(oAdapter)
' Get Delete Command Object
oAdapter.DeleteCommand =
oBuild.GetDeleteCommand()
' Submit DELETE through Adapter
oAdapter.Update(moDS, "CourseList")
' Tell DataSet changes to data source are
complete
moDS.AcceptChanges()
' Reload the list box
CourseListLoad()
Catch oException As Exception
MessageBox.Show(oException.Message)
End Try
End Sub