ERROR: Index (zero based) must be greater than or equal to zero and less than the size of the argument list
I'm trying to insert data in to my Database, here's the command:
public static bool InsertUser(string Date, string Day, string HourFrom, string HourTo, int HoursWorked, int Where)
{
string sql = string.Format("INSERT INTO Tablee (Date, Day, Hour开发者_Python百科From, HourTo, HoursWorked, Where)VALUES('{0}','{1}','{2}','{3}','{4}',{5},{6})", Date, Day, HourFrom, HourTo, HoursWorked, Where);
return DAL.ExecuteNonQuery(sql) != 0;
}
Now I'm getting the error which I wrote in the topic and its from the line where I built my String Sql (the "INSERT INTO..."). Anyone have an idea how I might be able to fix it?
P.S. What is the purpose of the '{0}'.'{1}'..? I copied it from an old project I did and I do not remember why its in there and what it does.
You have SEVEN indexes listed (0-6) and only 6 arguments.
Remove the {6}
.
having this call:
string.Format("...{0}','{1}','{2}','{3}','{4}',{5},{6}...", parameters...);
you should provide 7 parameters, from 0 to 6, at the moment you are providing only 6:
Date, Day, HourFrom, HourTo, HoursWorked, Where
and this is the error.
A bit of creative formatting illustrates your problem:
VALUES(
'{0}', '{1}', '{2}', '{3}', '{4}', {5}, {6})"
Date, Day, HourFrom, HourTo, HoursWorked, Where <<whoops
Either remove the "6" or provide it with a value.
精彩评论