469,926 Members | 2,212 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,926 developers. It's quick & easy.

"Invalid attempt to read when no data is present"

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

Aug 31 '06 #1
4 3186
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" wrote:
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

Aug 31 '06 #2
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

Peter wrote:
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" wrote:
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
Aug 31 '06 #3
On 31 Aug 2006 14:29:52 -0700, "MarkusR" <ma*******@gmail.comwrote:
>Hey Peter,

It looks like I need:

reader.Read();

above:
if (reader.HasRows)
<snip>

If you put it above if(reader.HasRows), it will throw an error if there are no
rows. It should be like this:

If(reader.HasRows)
{
reader.Read();
..
..
..
}
Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Sep 1 '06 #4
thanks Otis.

-Markus_R

Otis Mukinfus wrote:
On 31 Aug 2006 14:29:52 -0700, "MarkusR" <ma*******@gmail.comwrote:
Hey Peter,

It looks like I need:

reader.Read();

above:
if (reader.HasRows)
<snip>

If you put it above if(reader.HasRows), it will throw an error if there are no
rows. It should be like this:

If(reader.HasRows)
{
reader.Read();
.
.
.
}
Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Sep 13 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by wiredog | last post: by
3 posts views Thread by blackpuppy | last post: by
15 posts views Thread by waltbrad | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.