索引(从零开始)必须大于或等于零

嘿,我不断收到一个错误:

索引(基于零)必须大于或等于零且小于参数列表的大小。

我的代码:

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)); } 

第二个String.Format使用{2}作为占位符,但是只传入一个参数,所以应该使用{0}

改变这个:

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

对此:

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

在这一行中:

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

令牌{2}无效,因为您在parms中只有一个项目。 用这个代替:

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

改变这一行:

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

当试图抛出一个ArgumentException ,你无意中调用了ArgumentException构造函数重载时,也会发生这种情况

 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); } 
 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(); } } } 

图片