Object Reference not set to an instance error

G

Guest

I have this code in a test program I am playing with, but I receive an object
reference error when I click the Update, Add, or Delete buttons:

Declarations:

' Open a database connection.
Dim strConnection As String = _
"Data Source=localhost;Initial Catalog=Northwind;" _
& "Integrated Security=True"
Dim cn As SqlConnection = New SqlConnection(strConnection)
cn.Open( )

' Create a data adapter object and set its SELECT command.
Dim strSelect As String = _
"SELECT * FROM Categories"
Dim da As SqlDataAdapter = New SqlDataAdapter(strSelect, cn)

' Set the data adapter object's UPDATE, INSERT, and DELETE
' commands. Use the SqlCommandBuilder class's ability to auto-
' generate these commands from the SELECT command.
Dim autogen As New SqlCommandBuilder(da)

' Load a data set.
Dim ds As DataSet = New DataSet( )
da.Fill(ds, "Categories")

' Get a reference to the "Categories" DataTable.
Dim dt As DataTable = ds.Tables("Categories")

Update Button

' Modify one of the records.
Dim row As DataRow = dt.Select("CategoryName = 'Dairy Products'")(0)
row("Description") = "Milk and stuff"

' Update the database.
da.Update(ds, "Categories")

' Close the database connection.
cn.Close( )

Add Button

' Add a record.
row = dt.NewRow( )
row("CategoryName") = "Software"
row("Description") = "Fine code and binaries"
dt.Rows.Add(row)

' Update the database.
da.Update(ds, "Categories")

' Close the database connection.
cn.Close( )

Delete Button

' Delete a record.
row = dt.Select("CategoryName = 'MyCategory'")(0)
row.Delete( )

' Update the database.
da.Update(ds, "Categories")

' Close the database connection.
cn.Close( )

Help!
 
W

W.G. Ryan eMVP

I can't tell for sure from here, but looks like a good job for some
assertions: Which particluar line is throwing that exception? Also, you
may want to just let the adapter Open and close the connections -otherewise
throw it in a try/catch/fiannly block so you make sure your connections
don't leak.

' Load a data set.
Dim ds As DataSet = New DataSet( )
da.Fill(ds, "Categories")
Debug.Assert(ds.Tables("Categories").Rows.Count > 0)
' Get a reference to the "Categories" DataTable.
Dim dt As DataTable = ds.Tables("Categories")

Update Button

' Modify one of the records.
Dim row As DataRow = dt.Select("CategoryName = 'Dairy Products'")(0)
row("Description") = "Milk and stuff"
Debug.Assert(ds <> NOthing)
' Update the database.
da.Update(ds, "Categories")

' Close the database connection.
cn.Close( )

Add Button

' Add a record.
row = dt.NewRow( )
row("CategoryName") = "Software"
row("Description") = "Fine code and binaries"
dt.Rows.Add(row)

' Update the database.
Debug.Assert(ds <> Nothing)
da.Update(ds, "Categories")

' Close the database connection.
cn.Close( )

Delete Button

' Delete a record.
row = dt.Select("CategoryName = 'MyCategory'")(0)
Debug.Assert(row <> NOthing)
row.Delete( )

' Update the database.
Debug.Assert(ds <> Nothing)
da.Update(ds, "Categories")

' Close the database connection.
cn.Close( )
 

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