object reference not set to an instance of an object

S

student

Hello all, I have this problem in this code I am studying.
I'm getting this error saying that Server Error 'in
'/mcsdWebApps/Chapter5/DBruntime' Application.
Object reference not set to an instance of an object. It says the error is
on line 39 which is
Dim rowInsert As DataRow = dsContacts.Tables("Contacts").NewRow
Thanks in advance for any help!

Here is the full code:
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e as System.EventArgs) Handles MyBase.Load
'1. Create the data connection.
Dim ContactMgmt As New SqlConnection _
("Server=(local);database=Contacts;Trusted_Connection=yes")
'Create an adapter.
Dim adptContactMgmt As New _
SqlDataAdapter("select * from Contacts", ContactMgmt)
'2. Create a data set.
Dim dsContacts As New DataSet()
'Set select command.
adptContactMgmt.SelectCommand.CommandText = _
"SELECT * FROM Contacts"
'3. Create, insert, delete, and update commands automatically.
Dim cmdContactMgmt As SqlCommandBuilder = New _
SqlCommandBuilder(adptContactMgmt)
'4. Create a new row.
'HERE'S WHERE I'M GETTING THE ERROR!
Dim rowInsert as DataRow = dsContacts.Tables("Contacts").NewRow
'Add data to fields in the row.
rowInsert("ContactId") = 3
rowInsert("FirstName") = "Mary"
rowInsert("LastName") = "Smith"
rowInsert("WorkPhone") = "(444) 222-2222"
'Add the row to the data set.
dsContacts.Tables("Contacts").Rows.Add(rowInsert)
'5. Update the database.
adptContactMgmt.Update(dsContacts.Tables("Contacts"))
End Sub
 
C

Chris Taylor

Hi,

The problem is that you have not filled your dataset so the dataset does not
have a table called Contacts.
Here is a modified version of the code that should get you going.

Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e as System.EventArgs) Handles MyBase.Load
'1. Create the data connection.
Dim ContactMgmt As New SqlConnection _
("Server=(local);database=Contacts;Trusted_Connection=yes")

'Create an adapter.
Dim adptContactMgmt As New _
SqlDataAdapter("select * from Contacts", ContactMgmt)

'2. Create a data set.
Dim dsContacts As New DataSet()

'Set select command. This is not needed since this was set when you
constructed the SqlDataAdapter

'3. Create, insert, delete, and update commands automatically.
Dim cmdContactMgmt As SqlCommandBuilder = New _
SqlCommandBuilder(adptContactMgmt)

'*** The missing part, Fill the dataset using the DataAdapter and pass the
table name that you want
' the DataAdapter to use.
adptContactMgmt.Fill(dsContacts, "Contacts")

'4. Create a new row.
Dim rowInsert as DataRow = dsContacts.Tables("Contacts").NewRow
'Add data to fields in the row.
rowInsert("ContactId") = 3
rowInsert("FirstName") = "Mary"
rowInsert("LastName") = "Smith"
rowInsert("WorkPhone") = "(444) 222-2222"
'Add the row to the data set.
dsContacts.Tables("Contacts").Rows.Add(rowInsert)
'5. Update the database.
adptContactMgmt.Update(dsContacts.Tables("Contacts"))
End Sub


Hope this helps
 
S

student

Thanks for the help that got rid of the error, only now I'm getting a blank
screen when I view the code in a browser Internet Explorer, any idea as to
what is causing this??
thanks,
student
 
C

Chris Taylor

Hi,

I do not see what data control you are using, but one thing with ASP.NET is
that once you have setup the databinding you have to call DataBind()
otherwise no binding happens.

Hope this helps
 

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

Members online

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top