"Invalid attempt to read when no data is present"

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
 
G

Guest

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
 
M

MarkusR

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top