Solved

Check if dataset is null

Posted on 2006-07-16
10
463 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

856 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