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

Hi, I am getting an error:

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

My code is:

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)); } 
+82
c # sql mysql
Mar 15 '11 at 18:39
source share
5 answers

The second String.Format uses {2} as a placeholder, but you only pass one argument, so use {0} instead.

Change this:

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

For this:

 String.Format("{0}", reader.GetString(2)); 
+138
Mar 15 '11 at 18:41
source share

In this line:

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

The token {2} is invalid because you have only one item in parms. Use this instead:

 Aboutme.Text = String.Format("{0}", reader.GetString(0)); 
+19
Mar 15 '11 at 18:41
source share

Change this line:

 Aboutme.Text = String.Format("{0}", reader.GetString(0)); 
+7
Mar 15 '11 at 18:41
source share

This can also happen when you try to throw an ArgumentException , where you inadvertently throw an ArgumentException constructor overload

 public static void Dostuff(Foo bar) { // this works throw new ArgumentException(String.Format("Could not find {0}", bar.SomeStringProperty)); //this gives the error throw new ArgumentException(String.Format("Could not find {0}"), bar.SomeStringProperty); } 
+1
Mar 31 '14 at 8:04
source share
 using System; namespace ConsoleApp1 { class Program { static void Main() { Console.WriteLine("Enter Your FirstName "); String FirstName = Console.ReadLine(); Console.WriteLine("Enter Your LastName "); String LastName = Console.ReadLine(); Console.ReadLine(); Console.WriteLine("Hello {0}, {1} " + FirstName, LastName); Console.ReadLine(); } } } 

Picture

0
Sep 28 '17 at 14:01
source share



All Articles