0

In my if statement below I need to check if in the first tables first row/column contains a string but I get an exception if the table has no rows.

the exception is:

"System.IndexOutOfRangeException = {"There is no row at position 0."}"

Code snippet:

'if the table has no rows then an exception happens here
If myDataSet.Tables(0).Rows(0)(0).ToString <> "MyMessage" then


'do this - redirect

Else

 myDataSet.Tables(0).Rows(0)(0) = "no message"

End If

Can you help please?

1
  • what's the exception message? Commented Jan 8, 2013 at 7:20

4 Answers 4

2

It throws an exception because you're assuming a row exists with the following statement If myDataSet.Tables(0).Rows(0)(0)

You should check that you have a row first by doing If myDataSet.Tables(0).Rows.Count > 0

Sign up to request clarification or add additional context in comments.

Comments

1

Wrap your if..else block inside an if as:

if myDataSet.Tables(0).Rows.Count > 0 then 
//your code here
end if

Comments

1

You cannot access myDataSet.Tables(0).Rows(0) if there's no row in the resultset. Check the row count first:

If myDataSet.Tables.Count <> 0 AND myDataSet.Tables(0).Rows.Count <> 0 Then
    ' your code
End If

Comments

0

You should first check if your table has any rows

If myDataSet.Tables(0).Rows.Count <> 0 then

'do stuff

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.