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
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