Solved

Check if dataset is null

Posted on 2006-07-16
10
445 Views
Last Modified: 2008-03-06
I want to check that the dataset is not null before performing my operations.  Here's what I have so far:

         //create an instance of the GetListURLs class first
        GetListURLs geturls = new GetListURLs(System.Configuration.ConfigurationManager.AppSettings["MyConn"]);

        DataSet ds = geturls.GetList();

        //use the geturls class object to call GetList() which returns a dataset and use that as the DataSource for GridView
        if (ds.Tables[0].Rows.Count > 0)
        {
            VPPURLList.DataSource = geturls.GetList();
            VPPURLList.DataBind();
        }
        else
        {
            ErrorMsg.Text = "Unable to connect to the database.";
        }

However, I get this error:

Object reference not set to an instance of an object

for this line:

        if (ds.Tables[0].Rows.Count > 0)

0
Comment
Question by:dba123
10 Comments
 
LVL 10

Accepted Solution

by:
orbulat earned 500 total points
ID: 17119453
how about this one?

if (ds != null)
{
   if (ds.Tables[0].Rows.Count > 0)
        {
            VPPURLList.DataSource = geturls.GetList();
            VPPURLList.DataBind();
        }
        else
        {
            ErrorMsg.Text = "Unable to connect to the database.";
        }
}
else
{
     ErrorMsg.Text = "Unable to connect to the database.";
}
0
 
LVL 1

Author Comment

by:dba123
ID: 17119464
it's not treating my dataset as an object is the problem here.
0
 
LVL 10

Expert Comment

by:orbulat
ID: 17119491
um... then maybe u can use this
try
{
        if (ds.Tables[0].Rows.Count > 0)
        {
            VPPURLList.DataSource = geturls.GetList();
            VPPURLList.DataBind();
        }
        else
        {
            ErrorMsg.Text = "Unable to connect to the database.";
        }
}
catch
{
    ErrorMsg.Text = "Unable to connect to the database.";
}
0
 

Expert Comment

by:tfountain
ID: 17119553
You need to initialize the dataset first.  Try:
        //create an instance of the GetListURLs class first
        GetListURLs geturls = new GetListURLs(System.Configuration.ConfigurationManager.AppSettings["MyConn"]);
 //****changed here  
        DataSet ds = new DataSet;
       ds = geturls.GetList();
//****end changed
        //use the geturls class object to call GetList() which returns a dataset and use that as the DataSource for GridView
        if (ds.Tables[0].Rows.Count > 0)
        {
            VPPURLList.DataSource = geturls.GetList();
            VPPURLList.DataBind();
        }
        else
        {
            ErrorMsg.Text = "Unable to connect to the database.";
        }
0
 
LVL 1

Author Comment

by:dba123
ID: 17119624
doesn't this already initialize the ds dataset all in one line: DataSet ds = geturls.GetList();
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 12

Expert Comment

by:deanvanrooyen
ID: 17119898
this relates to the query

 GridView Not Working - NO DATA
0
 
LVL 7

Expert Comment

by:pradeepsudharsan
ID: 17120510
 //create an instance of the GetListURLs class first
        GetListURLs geturls = new GetListURLs(System.Configuration.ConfigurationManager.AppSettings["MyConn"]);

        DataSet ds = geturls.GetList();

        //use the geturls class object to call GetList() which returns a dataset and use that as the DataSource for GridView
        if (ds!=null | ds.Tables[0].Rows.Count > 0)
        {
            VPPURLList.DataSource = geturls.GetList();
            VPPURLList.DataBind();
        }
        else
        {
            ErrorMsg.Text = "Unable to connect to the database.";
        }
0
 
LVL 1

Author Comment

by:dba123
ID: 17123391
again, it's not recognizing ds as a dataset due to something wrong with my class
0
 
LVL 1

Author Comment

by:dba123
ID: 17123405
I notice I'm getting a connection string problem

 I found out that my Connection couldn't be opened after taking out the catch portion in my GetListURLs class.  I got this error in my class now because the connection isn't liking my domain login for sql server.  My domain login in SQL 2005 has db_owner rights on the database in question:

Exception Details: System.Data.SqlClient.SqlException: Cannot open database "dbname" requested by the login. The login failed.
Login failed for user 'domain\myaccount'.

Source Error:

Line 25:             try
Line 26:             {
Line 27:                 Conn.Open();
Line 28:                 SqlCommand GetURLsCommand = new SqlCommand("GetURLs", Conn);
Line 29:                 GetURLsCommand.CommandType = CommandType.StoredProcedure;

Again, as you will find in my web.config:

      <connectionStrings>
            <add name="MyConn"
         connectionString="Data Source=.;
           Initial Catalog=MyTestData;
               Integrated Security=True;"/>
      </connectionStrings>
0
 
LVL 1

Author Comment

by:dba123
ID: 17123710
Finally,  I changed the connection string to use the correct SQL instance.  I didn't have it named as local in SQL 2005 Management Studio, I had it actually as pcname\LOCAL

that fixed it!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now