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
 
G

Guest

Hi,

I found that I actually still have that error, I think it could be possibly
complicated by the way my table is designed. I will try to explain in more
detail below.

My sql statement is:
strCommand = " SELECT studentAnswer2 +
" FROM StudentAnswer "+
" WHERE studentId4 " +
" AND testpaperId1 ";

My sql result using the query analyzer is:
studentAnswer2
==========
NULL

This is due to the fact that my table columns consists of studentAnswer1,
studentAnswer2, studentAnswer3 etc. Therefore when studentAnswer1 is
inserted, studentAnswer2 and studentAnswer3 and the rest of the row displays
NULL.

if (ds.Tables["TestQn"].Rows.Count > 0)
{
string strAnswer = (string)ds.Tables["TestQn"].Rows[0]["studentAnswer2"];
some processing;
}
catch (Exception ex)
{ lbl2.Text = ex.Message; }

Obviously the count doesn't work as as long as there is studentAnswer1
inserted, the count is always > 0.
I find that when the debugger hits the "string strAnswer = ...", an
exception is thrown. Error msg: "The specified cast is not available". What
does this mean ?
What I dun understand is why:
if ( ds.Tables["testQuestion"+ intquestionId].Rows[0][studentAnswer2 ] !=
null )
doesn't work either ?

TIA.
Andrew.


S. Justin Gengo said:
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
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

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top