invalid attempt to read when no data is present, c#

Posted on 2014-08-09
Last Modified: 2014-08-09
Question: How can I correct the folowing error at line 18?

Error: invalid attempt to read when no data is present

After I changed lines 10 and 17, respectively, to read:

command.CommandText = "SELECT * From RodCtiteria";

if (reader.GetString(4) != null)

I got the error shown on the attached image with more detailed info.

public string ReadCriteria(string dataPoint)
        string criteria = "";
        using (SqlConnection connection = new SqlConnection("Data Source=USER-PC;Initial Catalog=ROD_July18;Integrated Security=True"))
            SqlCommand command = new SqlCommand();

            //string YYYY = rc.YYYY;// Session["YYYY"].ToString();
            command.Connection = connection;
            command.CommandText = "SELECT Top 1 [" + dataPoint + "] From RodCtiteria";
            command.CommandType = CommandType.Text;

            SqlDataReader reader = command.ExecuteReader();
            if (reader.HasRows)
                if (reader.GetString(0) != "")
                    criteria = reader.GetString(0).ToString();     //<-- error here****************************
        return criteria;

Open in new window

Question by:Mike Eghtebas
    LVL 35

    Accepted Solution

    You forgot to actually Read() the data, also there is a function for testing null:
                    if (reader.HasRows) {
                        if (!reader.IsDBNull(0)) // instead of: reader.GetString(0) != ""
                            criteria = reader.GetString(0);

    Open in new window

    LVL 74

    Assisted Solution

    by:käµfm³d 👽
    And really there is no reason to even check HasRows:

    SqlDataReader reader = command.ExecuteReader();
    if (reader.Read())
        (reader.GetString(0) != "")

    Open in new window

    LVL 69

    Assisted Solution

    by:Éric Moreau
    since you read a single column from a single row, you should use ExecuteScalar instead of Reader. It is optimized for that kind of read:
    LVL 33

    Author Closing Comment

    by:Mike Eghtebas
    Thank you.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    729 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

    18 Experts available now in Live!

    Get 1:1 Help Now