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