Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Check if dataset is null

Posted on 2006-07-16
10
Medium Priority
?
485 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

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.

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

721 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