This:
if (comboBox1.SelectedIndex != 0)
{
this.comboBox1.Items.Add(this.comboBox1.SelectedIndex);
}
is not a loop. It's an if-Statement. The code in the block of the if-Statement gets executed
once if the predicate of the if-Statement evaluates to true.
I assume you want to insert the newly created record as a combobox-item in any case, right? Then you won't need an if-Statement. Currently you only would insert the new combobox-item if there's any item currently selected. And currently you actually insert the SelectedIndex of the combobox - as you can easily read from your code. That doesn't make much sense. I assume you would want something like this:
comboBox1.Items.Add(String.Concat(textBox1.Text, " ,", dateTimePicker1.Text, " ,", textBox3.Text));
And then there are a couple of other problems with your code:
- Use SQL-Parameters.
Simple example here[
^]
- If an exception occurs you report it and then keep going as if nothing happened. You should do something to handle the error besides reporting it.
- Your connection won't get closed in case of an exception being thrown.
edits: typos