Solved

Data reader and NULL values

Posted on 2011-02-26
6
434 Views
Last Modified: 2012-05-11
Hi,
I am using data reader for reading data from the table. For this particular case no record was found and I am getting the following error. InvalidOperationException was unhandled by user code
Invalid attempt to read when no data is present.
 In reality I am looking for both values then I have data and when there is none. How can I handle this problem that it would not throw an exception? Thanks!

protected void CustomerLevel()
 {
  SqlConnection Conn = new SqlConnection(ConnString);

 SQL = "Select level from CustomerTable where Name = '" + CustName + "'";

      SqlCommand cmd = new SqlCommand(SQL, objConn);
      SqlDataReader r;

       Conn.Open();
        r = cmd.ExecuteReader();
        r.Read();

            if (!(r["level"] == DBNull.Value))
            {
                 Custlevel = r["level"].ToString();
            }
            else
            {
                Custlevel = "";
}
r.Close();
Conn.Close();

level = Custleve;
HttpCookie Cookie1 = new HttpCookie("level", level);
Response.Cookies.Add(Cookie1);
          }
0
Comment
Question by:avi7
  • 3
  • 3
6 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 34989996
Hi avi7;

Test to see if the reader has rows before reading.

    Conn.Open();
    r = cmd.ExecuteReader();
    
    // Test to see if SqlDataReader has data to be read.
    if( r.HasRows )
    {
        r.Read();
    }
    else
    {
        // SqlDataReader has no rows no data was returned.
        return;
    }

    if (!(r["level"] == DBNull.Value))

Open in new window


Fernando
0
 

Author Comment

by:avi7
ID: 34990001
Thanks!
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 34990052
Did this work for you?
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:avi7
ID: 34990228
Let me ask you... did you get any point or did I forgot to assign them to you?
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 34990270
You did just assigned them, thanks. In the future you should assign points to the post that answered the question this way when others search for an answer to the same question the correct post will be easy to find.

Thanks;
Fernando
0
 

Author Comment

by:avi7
ID: 34990333
Thanks again!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

20 Experts available now in Live!

Get 1:1 Help Now