Solved

Data reader and NULL values

Posted on 2011-02-26
6
441 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 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

840 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