?
Solved

Data reader and NULL values

Posted on 2011-02-26
6
Medium Priority
?
448 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
[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
  • 3
  • 3
6 Comments
 
LVL 63

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 63

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 34990052
Did this work for you?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 63

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

777 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