IndexOutofRangeException :There is no row at position 0

hshantanu picture hshantanu · Apr 16, 2014 · Viewed 7.3k times · Source

I am trying to design a mini project of a quiz application using resource bundle. I get an error of There is no row at position 0 when I insert the following if condition. Where I am catching the string from combobox in variable X.

SampleResourceBundle.Loginpage l = new SampleResourceBundle.Loginpage();
if(l.x.Equals("mr_IN"))
   sql = "select * from quesans where qid>48";
else if (l.x.Equals("en_US"))
   sql = "select * from quesans where qid<48";

The error is given in the following statement in the method from the next form:

textBox1.Text = ds.Tables["QA"].Rows[recno].ItemArray[2].ToString();

But if I remove the if statement the code executes perfectly fine.

Answer

Szymon picture Szymon · Apr 16, 2014

There are no rows in ds.Tables["QA"].

You need to check if there is a row you want to read before attempting it:

if (recno < ds.Tables["QA"].Rows.Count)
    textBox1.Text = ds.Tables["QA"].Rows[recno].ItemArray[2].ToString();