[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Data reader and NULL values

Posted on 2011-02-26
6
Medium Priority
?
451 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 64

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 64

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 64

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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!
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

649 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