lkj
lkj

Reputation: 1033

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, HourFrom, 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.

Upvotes: 0

Views: 6229

Answers (3)

JosephStyons
JosephStyons

Reputation: 58755

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.

Upvotes: 1

Davide Piras
Davide Piras

Reputation: 44605

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.

Upvotes: 2

JNK
JNK

Reputation: 65187

You have SEVEN indexes listed (0-6) and only 6 arguments.

Remove the {6}.

Upvotes: 2

Related Questions