Monday, March 26, 2012

object reference not set to an instance of an object

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 SubHi,
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
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor
"student" <mr_chat123@.yahoo.com> wrote in message
news:24HBd.9293$by5.9098@.newssvr19.news.prodigy.com...
> 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
>
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
"Chris Taylor" <chris_taylor_za@.hotmail.com> wrote in message
news:%23TeGJtG8EHA.2700@.TK2MSFTNGP14.phx.gbl...
> 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
> --
> Chris Taylor
> http://dotnetjunkies.com/weblog/chris.taylor
> "student" <mr_chat123@.yahoo.com> wrote in message
> news:24HBd.9293$by5.9098@.newssvr19.news.prodigy.com...
is
>
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
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor
"student" <mr_chat123@.yahoo.com> wrote in message
news:ZQ_Bd.9661$by5.495@.newssvr19.news.prodigy.com...
> 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
> "Chris Taylor" <chris_taylor_za@.hotmail.com> wrote in message
> news:%23TeGJtG8EHA.2700@.TK2MSFTNGP14.phx.gbl...
> not
> the
error
> is
>

0 comments:

Post a Comment