473,399 Members | 3,302 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

ado.net error handling

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

Nov 19 '05 #1
6 2035
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" <An****@discussions.microsoft.com> wrote in message
news:33**********************************@microsof t.com...
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

Nov 19 '05 #2
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" <An****@discussions.microsoft.com> wrote in message
news:33**********************************@microsof t.com...
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

Nov 19 '05 #3
if(.... .Count > 0)

--
Curt Christianson
site: http://www.darkfalz.com
blog: http://blog.darkfalz.com

"Andrew" wrote:
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

Nov 19 '05 #4
Thanks, solved the problem.

"Marina" wrote:
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" <An****@discussions.microsoft.com> wrote in message
news:33**********************************@microsof t.com...
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


Nov 19 '05 #5
> 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" <so*****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl... 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" <An****@discussions.microsoft.com> wrote in message
news:33**********************************@microsof t.com...
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


Nov 19 '05 #6
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" wrote:
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" <An****@discussions.microsoft.com> wrote in message
news:33**********************************@microsof t.com...
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


Nov 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: WSeeger | last post by:
When creating a new class, is it encouraged to always include error handling routines within your LET and GET procedures? It's seems that most text books never seem to include much about error...
12
by: Christian Christmann | last post by:
Hi, assert and error handling can be used for similar purposes. When should one use assert instead of try/catch and in which cases the error handling is preferable? I've read somewhere that...
6
by: Squirrel | last post by:
I have a command button on a subform to delete a record. The only statement in the subroutine is: DoCmd.RunCommand acCmdDeleteRecord The subform's recordsource is "select * from tblVisit order...
13
by: Thelma Lubkin | last post by:
I use code extensively; I probably overuse it. But I've been using error trapping very sparingly, and now I've been trapped by that. A form that works for me on the system I'm using, apparently...
21
by: Anthony England | last post by:
Everyone knows that global variables get re-set in an mdb when an un-handled error is encountered, but it seems that this also happens when the variable is defined as private at form-level. So...
3
by: Stefan Johansson | last post by:
Hi all I'am moving from Visual Foxpro and have a question regarding "best practice" error handling in vb .net. In VFP I have always used a "central" error handling object in order to have a...
4
by: Al Williams | last post by:
Hi, I have error handling in place throughout my application. I also start the application wrapped in error handling code to catch any unexpected exceptions (i.e. exceptions that occur where I...
10
by: Anthony England | last post by:
(sorry for the likely repost, but it is still not showing on my news server and after that much typing, I don't want to lose it) I am considering general error handling routines and have...
0
by: Lysander | last post by:
Thought I would give something back with a few articles. This article is a bit of code to add error handling. When I have time, I want to write articles on multilingual databases, and Access...
9
by: MrDeej | last post by:
Hello guys! We have an SQL server which sometimes makes timeouts and connection errors. And we have an function witch writes and updates data in 2 tables on this server. When the SQL server error...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.