Reputation: 9
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
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
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
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