mohammadt
mohammadt

Reputation: 9

ask about this error:Additional information: Index (zero based) must be greater than or equal to zero and less than the size of the argument list

This is my first time.Hi,I'm working on a SQL project and write a C# program for retrieve it.But I can't solve this error. please help me.Thanks.here my code:

 private void Insert_Click(object sender, EventArgs e) {   

        var db = new DataClasses1DataContext();

        db.ExecuteCommand("INSERT INTO Students VALUES ({105},{ali},{askari},  {[email protected]},{091345})",

        new object[] { textBox1.Text, textBox2.Text, textBox3.Text, textBox5.Text, textBox6.Text });

        dataGridView1.DataSource = db.Students;

 }

Upvotes: 0

Views: 83

Answers (3)

Henk Holterman
Henk Holterman

Reputation: 273524

The parameter numbering is position based:

//db.ExecuteCommand("INSERT INTO Students VALUES ({105},{ali},{askari},  {[email protected]},{091345})",
 db.ExecuteCommand("INSERT INTO Students VALUES ({0},{1},{2},{3},{4})",
   new object[] { textBox1.Text, textBox2.Text, textBox3.Text, textBox5.Text, textBox6.Text });

It is unclear what you want the {105},{ali},{askari}, ... values waere meant to be.

You may also need quotes around your string values, like:

 db.ExecuteCommand("INSERT INTO Students VALUES ({0}, \"{1}\", \"{2}\", \"{3}\", \"{4}\")",

Upvotes: 1

DGibbs
DGibbs

Reputation: 14618

ExecuteCommand takes a params[] object and a format string. It looks like this is what you're attempting to use.

In which case you have it wrong and it should be:

db.ExecuteCommand("INSERT INTO Students VALUES ({0},{1},{2},{3},{4})", 
                              105, "ali", "askari", "[email protected]", 091345);

Upvotes: 1

asafrob
asafrob

Reputation: 1858

You should change

INSERT INTO Students VALUES ({105},{ali},{askari}, {[email protected]},{091345} to

"INSERT INTO Students VALUES ({0},{1},{2},{3},{4})"

This is a simple string format issue where the arguments need to be in that format.

Upvotes: 1

Related Questions