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.
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();
}
}
}
In my case I could not see the mistake "+name". The compiler would not report an error in this case. So take care.
//Wrong Code:
string name="my name";
string age=25;
String.Format(@"Select * from table where name='{1}' and age={1}" +name, age);
//Right Code:
string name="my name";
string age=25;
String.Format(@"Select * from table where name='{1}' and age={1}" , name, age);
In this line:
Aboutme.Text = String.Format("{2}", reader.GetString(0));
The token {2} is invalid because you only have one item in the parms. Use this instead:
Aboutme.Text = String.Format("{0}", reader.GetString(0));
String.Format must start with zero index "{0}" like this:
Aboutme.Text = String.Format("{0}", reader.GetString(0));
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));
This can also happen when trying to throw an ArgumentException
where you inadvertently call the 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);
}