Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Check if dataset is null

Posted on 2006-07-16
10
Medium Priority
?
489 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 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

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…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

824 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