[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


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

Posted on 2014-08-09
Medium Priority
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

Robert Schutt earned 1000 total points
ID: 40250456
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 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 600 total points
ID: 40250671
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 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 400 total points
ID: 40250689
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: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar(v=vs.110).aspx
LVL 34

Author Closing Comment

by:Mike Eghtebas
ID: 40250839
Thank you.

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

834 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