-1

i wanna do a combobox for the classes , when i insert the type of class in it i want to selecteditem the row that was selected exmple: i chose second row then i insert to the database a 2

               sql = "insert into etudiant  (Nom, prenom, sexe,classess) " +
                    "values(@Nom, @prenom,  @sexe,@classess)"/*+"class (nom_class)" + "values(@nom_class) "*/;
                   cmd = new NpgsqlCommand(sql, conn);
               cmd = new NpgsqlCommand(sql, conn);
               cmd.Parameters.AddWithValue("@nom", bunifuMaterialTextbox1.Text);
               cmd.Parameters.AddWithValue("@prenom", bunifuMaterialTextbox2.Text);
               cmd.Parameters.AddWithValue("@sexe", bunifuMaterialTextbox3.Text);

               int id = comboBox1.SelectedIndex + 1;

                cmd.Parameters.AddWithValue("@classess",Int32.Parse(comboBox1.SelectedItem.ToString()));//*/

               //cmd.Parameters.AddWithValue("Num", bunifuMaterialTextbox6.Text);
               //cmd.Parameters.AddWithValue("Classess", bunifuMaterialTextbox7.Text);
               int result = cmd.ExecuteNonQuery();

i couldn't find a way to do it like that i wanna find a way so i can insert to table classes int for the number of that row like the ID

  • Hoe are you populating the combobox? What value are you getting in `combobox1.SelectedItem`? What's the issue you are facing with this code? – Chetan Jun 04 '20 at 12:22
  • Also specify if you are in WPF/Winforms or what! Why not using the selectedvalue item of the combo? – Emanuele Jun 04 '20 at 12:23
  • @ChetanRanpariya from this `````` conn = new NpgsqlConnection(connstring); conn.Open(); sql = @"select * from class"; cmd = new NpgsqlCommand(sql, conn); dt = new DataTable(); dt.Load(cmd.ExecuteReader()); foreach (DataRow dr in dt.Rows) { comboBox1.Items.Add(dr["nom_class"].ToString()); } conn.Close();`````` and @emanuele it's Winforms – Yassine Fatnassi Jun 04 '20 at 12:35
  • What the issue you are facing? – Chetan Jun 04 '20 at 12:37
  • Well the probleme that i have that i wanna put each line in my combobox a number to insert it to database for example when i chose in combobox country i chose england i insert in the database a number 5 – Yassine Fatnassi Jun 04 '20 at 12:42

1 Answers1

1

For your question, you would like to achieve the current combobox selected index and insert

it into your database.

First, I want to mention that you should use comboBox1.SelectedIndex instead of comboBox1.SelectedItem.

Second, I use sqlconnection to complete the same operation, so you can modify the following code to apply to your code.

  public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string connstr = @"";
            SqlConnection connection = new SqlConnection(connstr);
            connection.Open();
            string sql = "insert into Example(Nom,Prenom,Sexe,Classess) values(@Nom,@Prenom,@Sexe,@Classess)";
            SqlCommand command = new SqlCommand(sql, connection);
            command.Parameters.AddWithValue("@Nom", textBox1.Text);
            command.Parameters.AddWithValue("@Prenom", textBox2.Text);
            command.Parameters.AddWithValue("@Sexe", textBox3.Text);
            command.Parameters.AddWithValue("@Classess", comboBox1.SelectedIndex+1);
            command.ExecuteNonQuery();
            MessageBox.Show("add value successfully"); 

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Add("class1");
            comboBox1.Items.Add("class2");
            comboBox1.Items.Add("class3");
            comboBox1.Items.Add("class4");
        }
    }
Jack J Jun
  • 5,633
  • 1
  • 9
  • 27