Solved

Data reader and NULL values

Posted on 2011-02-26
6
442 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 500 total points
ID: 34990052
Did this work for you?
0
Industry Leaders: 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!

 

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 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