Solved

Check if dataset is null

Posted on 2006-07-16
10
431 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 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

21 Experts available now in Live!

Get 1:1 Help Now