【问题标题】:changing my combobox list to number so i can insert it to database c#将我的组合框列表更改为数字,以便我可以将其插入数据库 c#
【发布时间】:2020-09-23 10:12:20
【问题描述】:

我想为类做一个组合框,当我在其中插入类的类型时,我想选择选中的行,例如:我选择了第二行,然后我向数据库中插入了 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();

我找不到这样的方法 我想找到一种方法,这样我就可以在表格类 int 中插入该行的编号,例如 ID

【问题讨论】:

  • 锄头你在填充组合框吗?您在combobox1.SelectedItem 中获得了什么价值?您在使用此代码时遇到了什么问题?
  • 还要指定你是在 WPF/Winforms 还是什么!为什么不使用组合的 selectedvalue 项?
  • @ChetanRanpariya 来自这个`````` conn = new NpgsqlConnection(connstring); conn.Open(); sql = @"从类中选择 *"; cmd = new NpgsqlCommand(sql, conn); dt = 新数据表(); dt.Load(cmd.ExecuteReader()); foreach(dt.Rows 中的 DataRow dr){ comboBox1.Items.Add(dr["nom_class"].ToString()); } conn.Close();`````` 和 @emanuele 它是 Winforms
  • 您遇到了什么问题?
  • 好吧,我有一个问题,我想在我的组合框中为每一行添加一个数字以将其插入数据库,例如,当我在组合框国家/地区选择时,我选择了英国,我在数据库中插入了一个数字 5

标签: c# visual-studio visual-studio-code combobox selecteditem


【解决方案1】:

对于你的问题,你想实现当前组合框选中索引并插入

它进入你的数据库。

首先,我想提一下,您应该使用 comboBox1.SelectedIndex 而不是 comboBox1.SelectedItem。

其次,我使用sqlconnection完成同样的操作,所以你可以修改以下代码应用到你的代码中。

  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");
        }
    }

【讨论】:

  • 非常感谢它成功了,下次我将使用 selectedindex :)
  • @YassineFatnassi,我很高兴我的解决方案对你有用。我建议您可以将其标记为答案,谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-10
  • 2012-08-17
  • 2011-07-08
  • 1970-01-01
相关资源
最近更新 更多