Index (zero based) must be greater than or equal to zero

G Gr picture G Gr · Mar 15, 2011 · Viewed 208.9k times · Source

Hey I keep getting an error:

Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

My code:

OdbcCommand cmd = new OdbcCommand("SELECT FirstName, SecondName, Aboutme FROM User WHERE UserID=1", cn);

OdbcDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
    Name.Text = String.Format("{0} {1}", reader.GetString(0), reader.GetString(1));
    Aboutme.Text = String.Format("{2}", reader.GetString(0));
}

Answer

Ahmad Mageed picture Ahmad Mageed · Mar 15, 2011

Your second String.Format uses {2} as a placeholder but you're only passing in one argument, so you should use {0} instead.

Change this:

String.Format("{2}", reader.GetString(0));

To this:

String.Format("{0}", reader.GetString(2));