Link to home
Start Free TrialLog in
Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike Eghtebas

ASKER

Thank you.