1

I am trying to return a data object from my database so that i can access (for example) a customer ID within my ASP.NET website. Upon a customer logging in the object is returned. However, i am getting the error:

   'Invalid attempt to read when no data is present.' 

I have completed an sql query on the database (Executing my stored procedure) which returns the correct information, so i know it is there. I can only presume that there is something wrong with the following method:

    using (SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
        {
            using (SqlCommand sqlComm = new SqlCommand("Select_Customer_By_UserName_And_Password", sqlConn))
            {
                sqlComm.Connection.Open();
                try
                {
                    sqlComm.CommandType = CommandType.StoredProcedure;
                    sqlComm.Parameters.Add("@Username", SqlDbType.NVarChar, 25).Value = pUsername;
                    sqlComm.Parameters.Add("@Password", SqlDbType.NVarChar, 25).Value = pPassword;

                    using (SqlDataReader sqlDR = sqlComm.ExecuteReader(CommandBehavior.SingleRow))
                    {
                        if (sqlDR.HasRows)
                        {
                            //Creating the new object to be returned by using the data from the database.
                            return new Customer
                            {
                                CustomerID = Convert.ToInt32(sqlDR["CustomerID"])
                            };
                        }
                        else
                            return null;
                    }
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    sqlComm.Connection.Close();
                }
            }
        }

1 Answer 1

3

You need to call sqlDR.Read(), otherwise the "record pointer" will to point to a record. HasRows only indicates there are actually rows you can read. To read each row (or just the first one), you need to call Read once or in a while loop.

For example:

if (reader.HasRows)
{
    while (reader.Read())
        ...
}

Your code should read:

using (SqlDataReader sqlDR = sqlComm.ExecuteReader(CommandBehavior.SingleRow))
{
    if (sqlDR.Read())
    {
        //Creating the new object to be returned by using the data from the database.
        return new Customer
        {
            CustomerID = Convert.ToInt32(sqlDR["CustomerID"])
        };
    }
    else
        return null;
}

By the way: congrats on using using and parameterized queries!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.