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();
}
}
}