SqlDataReader

J

Justin Lazanowski

Ok this one has me stumped. Everything looks right, but for whatever reason
the DataReader keeps telling me not set to an instance of an object. When I
step through the code, it runs the line

SqlDataReader mySqlReader;

However I can see in the watch value that the mySqlReader is set to an
undefined value

When it gets to the mySqlReader = sc.ExecuteReader() command I get the not
set error

Anyone have any thoughts?

Code below
<code>

int [] results;

results = new int[5];


SqlDataReader mySqlReader;

sc.Connection = sqlConn;

sc.CommandTimeout = 450;

try

{

sqlConn.Open();

mySqlReader = sc.ExecuteReader();

while(mySqlReader.Read())



</code>
 
J

Justin Lazanowski

No the sc is a SQL command that is set in the calling function.

All the properties are set, and as stated in the previous message, when I
step through the code after it runs the the SqlDataReader mySqlReader; the
value is still set as undefined.

That is why I am so confused.

Justin


Miha Markic said:
Hi Justin,

Have you forgot to set CommandText?

--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
Justin Lazanowski said:
Ok this one has me stumped. Everything looks right, but for whatever
reason the DataReader keeps telling me not set to an instance of an
object. When I step through the code, it runs the line

SqlDataReader mySqlReader;

However I can see in the watch value that the mySqlReader is set to an
undefined value

When it gets to the mySqlReader = sc.ExecuteReader() command I get the
not set error

Anyone have any thoughts?

Code below
<code>

int [] results;

results = new int[5];


SqlDataReader mySqlReader;

sc.Connection = sqlConn;

sc.CommandTimeout = 450;

try

{

sqlConn.Open();

mySqlReader = sc.ExecuteReader();

while(mySqlReader.Read())



</code>
 
J

Justin Lazanowski

Ok, I found the error.

Apperently the SP was returning a raiseerror and when I try and assign any
value or for that matter even run .ExecuteReader() it throws a general
Exception.

So this change the scope of the question a little bit

I have the .ExecuteReader() in a try catch finally block.

So How do I get it to throw the exception here as opposed to throwing a
gneral exeception that breaks the application?

I appreciate any help

The entire method appears below

System.Data.SqlClient.SqlDataReader dr;

int [] results;

results = new int[5];



sc.Connection = sqlConn;

sc.CommandTimeout = 450;

try

{

sqlConn.Open();


dr = sc.ExecuteReader();



while(dr.Read())

{

try

{

for (int i=0; i< dr.FieldCount; i++)

{

results = int.Parse(dr.ToString());

results = int.Parse(dr.ToString());

results = int.Parse(dr.ToString());

}

}

catch(System.Data.SqlClient.SqlException ex)

{

error = ex.ToString();

}

catch(System.Exception ex)

{

error = ex.ToString();

}



}

dr.Close();



}

catch (System.Data.SqlClient.SqlException ex)

{

error = ex.Message.ToString();

return null;

}

catch (System.Exception ex)

{

error = ex.Message.ToString();

return null;

}

finally

{

sqlConn.Close();


}

error = string.Empty;


return results;


}
 

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