"Invalid attempt to read when no data is present"

  • Thread starter Thread starter MarkusR
  • Start date Start date
M

MarkusR

If I run the stored proc in the Query Analyzer this works and I get the
expected result set back. However when I run it in my application I get
a results set of one row but when I try to access the values I get
"Invalid attempt to read when no data is present".

private void GetLotIDPriorityFromLot(string aLotDesc, out int aLotID,
out DateTime aPriorityDate)
{
SqlConnection Conn;
SqlDataReader reader = null;

aLotID = 0;
aPriorityDate = DateTime.Today;

Conn = new SqlConnection("user id=myuserid;"
+"password=mypwd;"
+"server=myserver;"
+"database=mydatabase;"
+"connection timeout=30");
Conn.Open();
try
{
SqlCommand cmd =
new SqlCommand("sp_GetLotIDPriorityFromLot",
Conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@lot", aLotDesc));
reader = cmd.ExecuteReader();

if (reader.HasRows)
{
---> aLotID =
Convert.ToInt32(reader[0].ToString());
aPriorityDate =
Convert.ToDateTime(reader[1].ToString());
}
}
finally
{
if (reader != null)
reader.Close();
if (Conn != null)
Conn.Close();
}
}

Any thoughts?

-Markus_R
 
Markus_R,
It might help a lot if you put a catch block in there between the try and
the finally and output the exception Message and StackTrace properties, say,
to the output window with Debug.WriteLine.

Peter
 
Hey Peter,

It looks like I need:

reader.Read();

above:
if (reader.HasRows)

I had to close/reopen VS2005 before even that would take. VS2005 must
have been in a bad mood. I even tried build/rebuild.

-Markus
Markus_R,
It might help a lot if you put a catch block in there between the try and
the finally and output the exception Message and StackTrace properties, say,
to the output window with Debug.WriteLine.

Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




MarkusR said:
If I run the stored proc in the Query Analyzer this works and I get the
expected result set back. However when I run it in my application I get
a results set of one row but when I try to access the values I get
"Invalid attempt to read when no data is present".

private void GetLotIDPriorityFromLot(string aLotDesc, out int aLotID,
out DateTime aPriorityDate)
{
SqlConnection Conn;
SqlDataReader reader = null;

aLotID = 0;
aPriorityDate = DateTime.Today;

Conn = new SqlConnection("user id=myuserid;"
+"password=mypwd;"
+"server=myserver;"
+"database=mydatabase;"
+"connection timeout=30");
Conn.Open();
try
{
SqlCommand cmd =
new SqlCommand("sp_GetLotIDPriorityFromLot",
Conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@lot", aLotDesc));
reader = cmd.ExecuteReader();

if (reader.HasRows)
{
---> aLotID =
Convert.ToInt32(reader[0].ToString());
aPriorityDate =
Convert.ToDateTime(reader[1].ToString());
}
}
finally
{
if (reader != null)
reader.Close();
if (Conn != null)
Conn.Close();
}
}

Any thoughts?

-Markus_R
 
Back
Top