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

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;

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

Open in new window

readerError.png
LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
käµfm³d 👽Commented:
And really there is no reason to even check HasRows:

SqlDataReader reader = command.ExecuteReader();

if (reader.Read())
{
    (reader.GetString(0) != "")

Open in new window

0
Éric MoreauSenior .Net ConsultantCommented:
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
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.