Reputation: 1
I have a form in which I have a combobox that is populated with the column names of a table I have a textbox I intend to input values, when I click on the save button I want the data to be inserted into the various column that have been selected.
here is my code.
private void button1_Click(object sender, EventArgs e)
{
try
{
myConnection.ConnectionString = "Data Source = AmiayaEjay-Vaio; Initial Catalog = RealTime; User ID = sa; Password = admin";
String combo1 = comboBox1.SelectedItem.ToString();
String combo2 = comboBox2.SelectedItem.ToString();
String combo3 = comboBox3.SelectedItem.ToString();
String combo4 = comboBox4.SelectedItem.ToString();
String combo5 = comboBox5.SelectedItem.ToString();
String combo6 = comboBox6.SelectedItem.ToString();
String combo7 = comboBox7.SelectedItem.ToString();
String combo8 = comboBox8.SelectedItem.ToString();
query1.CommandText = "insert into dbo.DepthTable ( '" + combo1 + "','" + combo2 + "','" + combo3 + "','" + combo4 + "','" + combo5 + "','" + combo6 + "' ,'" + combo7 + "','" + combo8 + "') values ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "')";
query1.CommandType = CommandType.Text;
query1.Connection = myConnection;
myConnection.Open();
query1.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
myConnection.Close();
}
I keep on getting error messages that I have invalid column names because the sql command can't see combo1-combo8 has a valid column name
Upvotes: 0
Views: 183
Reputation: 13286
Remove the "'" signs in the comboX.
"insert into dbo.DepthTable (" + combo1 + "," + combo2 + "," + combo3 + "," + combo4 + "," + combo5 + "," + combo6 + "," + combo7 + "," + combo8 + ") values ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "')";
Upvotes: 3