Object reference not set to an instance of an object.

B

bazzer

im filling a datset with names of films and need to get the number of
films in the dataset. basically i want to know if its empty. but when
i try and get the number of rows it gives me the error: "Object
reference not set to an instance of an object" . I get this error from
the line:"Label3.Text =
CalendarFilmsDataSet11.Tables("Customers").Rows.Count". and i get the
error whether the dataset has items in it or not. can anyone tell me
why this is happening.
this is my code:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

If Not Page.IsPostBack Then

Label2.Text = "Films Showing on " + Session("theShowDate")

CalendarFilmsDataSet11.Clear()

OdbcDataAdapter1.SelectCommand.Parameters("cinemaID").Value
= Session("theCinemaID")

OdbcDataAdapter1.SelectCommand.Parameters("showDate").Value
= Session("theShowDate")

OdbcDataAdapter1.Fill(CalendarFilmsDataSet11)

Label3.Text =
CalendarFilmsDataSet11.Tables("Customers").Rows.Count

ListBox1.DataBind()

End If

End Sub
 
A

Alvin Bruney

the correct way to access data in a dataset is to first test using if
statements
if(ds != null && ds.Tables != null && ds.Tables.Count > 0 etc

--
________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Professional VSTO.NET - Wrox/Wiley 2006
 
E

Erik Funkenbusch

the correct way to access data in a dataset is to first test using if
statements
if(ds != null && ds.Tables != null && ds.Tables.Count > 0 etc

Or, in VB, as the original author seems to be using

If not ds is Nothing AndAlso not ds.Tables is Nothing AndAlso
ds.Tables.Count > 0 ...
 
A

Alvin Bruney

right. 2.0 also gives VB the IsNot function as well
If ds IsNot Nothing

I'm not aware if there is a performance improvement though.
--
________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Professional VSTO.NET - Wrox/Wiley 2006
-------------------------------------------------------
 

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,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top