A
Assimalyst
Hi,
I am using a datareader to extract an integer value from an SQL table.
string strPDTNo = pdtDateCboBx.SelectedValue;
SqlCommand cmd = new SqlCommand("proc_GetSpirNo", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@pdtNo", strPDTNo));
SqlDataReader dr = cmd.ExecuteReader();
// count rows, helps deal with potential null entries
int i = 0;
while(dr.Read())
{
i++;
}
if (i!=0)
{
int intSpirNo = (int)dr["spirNo"];
.. . .
but when the "int intSpirNo = (int)dr["spirNo"];" statement is executed
i get an error "Invalid attempt to read when no data is present."
Though there is definately a single value read into the spirNo column,
i checked it with query analyser.
However, if i remove the while statement and set i=1 (so the if
statement runs)
it works as expected. But i need a way to tell if the datareader has
extracted null or more rows.
Any ideas?
Thanks
I am using a datareader to extract an integer value from an SQL table.
string strPDTNo = pdtDateCboBx.SelectedValue;
SqlCommand cmd = new SqlCommand("proc_GetSpirNo", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@pdtNo", strPDTNo));
SqlDataReader dr = cmd.ExecuteReader();
// count rows, helps deal with potential null entries
int i = 0;
while(dr.Read())
{
i++;
}
if (i!=0)
{
int intSpirNo = (int)dr["spirNo"];
.. . .
but when the "int intSpirNo = (int)dr["spirNo"];" statement is executed
i get an error "Invalid attempt to read when no data is present."
Though there is definately a single value read into the spirNo column,
i checked it with query analyser.
However, if i remove the while statement and set i=1 (so the if
statement runs)
it works as expected. But i need a way to tell if the datareader has
extracted null or more rows.
Any ideas?
Thanks