Delete record from SQL DB

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
 
V

Val Mazur

Hi,

It means that Find method did not find record with the specified ID and in
that case you will get Nothing. Since you do not check if record was found
you get error on the next step. You should check if oDR really has something

oDR = moDS.Tables("CourseList").Rows.Find(CInt (strID))
if not oDR is nothing then
oDR.Delete()
end if
 
M

Mahatma

Still did the same error, Nothing when i put mouse over
oDR.Delete()

Also says, strSQL Value is Nothing?

Can you help me at all?

Thanks
 
V

Val Mazur

Hi,

But if oDR is Nothing, and you placed condition, which I posted, then you
should not get inside to call Delete.
Is it not Nothing?
 
M

Mahatma

I dont quite understand what you mean.

I have a listbox named lstCourse and i just want the item
i select deleted from the CourseList table, this has
worked for me before.

Can i email you with more info?

Any help would be much appreciated.
 
V

Val Mazur

Hi,

Sure you could e-mail code, but please zip it. I have a limit, so big
attachments will not go through
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top