ado.net error handling

G

Guest

Hi,

I have this line of code:

string strAnswer =
(string)ds.Tables["testQuestion1"].Rows[0]["studentAnswer1"];

I got the error "There is no row at position 0", which is true coz the
database is currently empty. How do I handle the error for this ? I tried :

try {
if ( ds.Tables["testQuestion"+ intquestionId].Rows[0] != null )
{
string strAnswer =
(string)ds.Tables["testQuestion1"].Rows[0]["studentAnswer1"];
some processing;
}
}
catch (Exception ex)
{ lbl2.Text = ex.Message; }

This doesn't work as the debugger just goes skips past the code and head to
the catch handler.

TIA.
Andrew
 
S

S. Justin Gengo

Andrew an error is being generated in the "If" because the same error is
thrown when you try to access the row.

Instead of just checking if your row information is there, you first need to
check if any rows were returned at all.

if ( ds.Tables["testQuestion"+ intquestionId].Rows.Count > 0;)

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
M

Marina

Sorry, I don't understand, what is the problem?

When you have an error, the execution jumps to the catch handler. Which is
what you want.
 
G

Guest

Thanks, solved the problem.

Marina said:
Sorry, I don't understand, what is the problem?

When you have an error, the execution jumps to the catch handler. Which is
what you want.

Andrew said:
Hi,

I have this line of code:

string strAnswer =
(string)ds.Tables["testQuestion1"].Rows[0]["studentAnswer1"];

I got the error "There is no row at position 0", which is true coz the
database is currently empty. How do I handle the error for this ? I tried
:

try {
if ( ds.Tables["testQuestion"+ intquestionId].Rows[0] != null )
{
string strAnswer =
(string)ds.Tables["testQuestion1"].Rows[0]["studentAnswer1"];
some processing;
}
}
catch (Exception ex)
{ lbl2.Text = ex.Message; }

This doesn't work as the debugger just goes skips past the code and head
to
the catch handler.

TIA.
Andrew
 
K

Kevin Spencer

When you have an error, the execution jumps to the catch handler. Which is
what you want.

Generally speaking, exceptions should be avoided at all cost. Exceptions are
expensive; they slow down the application, and use resources, regardless of
whether or not they are handled. In addition, "logic by exception" can make
debugging a pain in the butt. I once had to try to debug some code written
by a person who used "logic by exception," and it was nearly impossible, as
his code threw literally thousands of exceptions, all nicely handled. I was
looking for an exception in the code, and told the debugger to break on all
exceptions, as it was a handled exception somewhere I knew not. After an
hour or so of hitting the F5 key because the current exception was not the
one I was looking for, I gave up, and tried a different approach, which also
took awhile, but was a bit faster. And the app ran slow as molasses. If
one's code can be written so that it doesn't throw exceptions, it is far
better.

In this case, all he has to do is check the count of the rows to avoid the
exception.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.

Marina said:
Sorry, I don't understand, what is the problem?

When you have an error, the execution jumps to the catch handler. Which is
what you want.

Andrew said:
Hi,

I have this line of code:

string strAnswer =
(string)ds.Tables["testQuestion1"].Rows[0]["studentAnswer1"];

I got the error "There is no row at position 0", which is true coz the
database is currently empty. How do I handle the error for this ? I tried
:

try {
if ( ds.Tables["testQuestion"+ intquestionId].Rows[0] != null )
{
string strAnswer =
(string)ds.Tables["testQuestion1"].Rows[0]["studentAnswer1"];
some processing;
}
}
catch (Exception ex)
{ lbl2.Text = ex.Message; }

This doesn't work as the debugger just goes skips past the code and head
to
the catch handler.

TIA.
Andrew
 

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