Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my forms I have combobox linked to sql server, the items in combobox is identity, two textbox and datetimepickers and button add ,when I press the button add at runtime i want to see the numbers of current adding information in combobox, please if you have any help answer me.

Thanks a lot.
Posted
Updated 22-Mar-15 5:00am
v2
Comments
Kornfeld Eliyahu Peter 22-Mar-15 10:10am    
Member 11545313 22-Mar-15 10:28am    
i tried in button added
try
{
cn.Open();
cmd.CommandText = ("insert into film (nom_film,date_sortie,duree)values('" + textBox1.Text+ "','"+this.dateTimePicker1.Text+"','"+textBox3.Text+"')");
cmd.Connection = cn;
cmd.ExecuteNonQuery();

//the problems is inside loop
if (comboBox1.SelectedIndex != 0)
{
this.comboBox1.Items.Add(this.comboBox1.SelectedIndex);
//comboBox1.Refresh();
}

textBox1.Clear();
textBox1.Focus();
textBox3.Clear();
MessageBox.Show("Film Ajouter Avec Succees", "insertion Filme", MessageBoxButtons.OK, MessageBoxIcon.Information);
cn.Close();
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message.ToString());
}
Afzaal Ahmad Zeeshan 22-Mar-15 11:03am    
You can add new controls to the Control collection.
Member 11545313 23-Mar-15 17:23pm    
Give me an exemple plz i need to handle this
Dave Kreskowiak 22-Mar-15 11:18am    
Do not post your question in multiple forums. All that does is make collaboration on an answer very difficult and can get you ignored.

1 solution

This:
C#
//the problems is inside loop
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:

C#
//no if-Statement here
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
 
Share this answer
 
v3
Comments
Member 11545313 23-Mar-15 7:40am    
i try it but , he added me all the information in combobox + exeption
(Conversion failed when converting the varchar value 'Money,2015-03-23 ,90min' to data type int.
i want to increase the insertion of field in combobox at runtime

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900