2个不同的ComboBox并使用2个不同的显示器,Value Member

时间:2018-04-22 21:28:10

标签: c# mysql visual-studio

当我执行此

    private void button1_Click(object sender, EventArgs e)
    {
        panel1.Visible = true;

        MySqlConnection baglanti = new MySqlConnection("Server=localhost;Database=mydb;Uid=root;Pwd='';");
        baglanti.Open();
        MySqlCommand komut = new MySqlCommand("select*from mydb.malzemeler", baglanti);
        MySqlDataReader oku = komut.ExecuteReader();
        DataTable dt = new DataTable();
        dt.Load(oku);

        comboBox2.DataSource = dt;
        comboBox2.DisplayMember = "malzemeisim";
        comboBox2.ValueMember = "idmalzemeler";

        MySqlCommand komut2 = new MySqlCommand("select*from mydb.santiye", baglanti);
        MySqlDataReader oku2 = komut2.ExecuteReader();

        DataTable dt2 = new DataTable();

        dt2.Load(oku2);
        comboBox1.DataSource = dt2;
        comboBox1.DisplayMember = "santiye_ad";
        comboBox1.ValueMember = "idsantiye";
        baglanti.Close();

 MySqlConnection baglanti = new MySqlConnection("Server=localhost;Database=mydb;Uid=root;Pwd='';");

        baglanti.Open();

        MySqlCommand komut = new MySqlCommand("insert into mydb.malkontrol(tarih,birimtur,harcanan,kalan,santiye_idsantiye,malzemeler_idmalzemeler) VALUES ('" + dateTimePicker1.Text + "','" + degiscekLabel.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + Convert.ToUInt32(comboBox1.Text) + "','" + Convert.ToUInt32(comboBox2.Text)+"')", baglanti);

        MessageBox.Show("Kayıt Başarıyla eklendi");

        komut.ExecuteNonQuery();

        baglanti.Close();

 MySqlConnection baglanti = new MySqlConnection("Server=localhost;Database=mydb;Uid=root;Pwd='';");

        baglanti.Open();

        string query = "SELECT malzemebirimtur FROM malzemeler WHERE malzemeisim= '" + comboBox2.Text + "'";

        MySqlCommand komut = new MySqlCommand(query, baglanti);

        MySqlDataReader dr = komut.ExecuteReader();

        while (dr.Read())
        {

            degiscekLabel.Text = dr.GetValue(0).ToString();

        }

Database-malkontrol表 enter image description here

**我收到此错误**

  

输入字符串的格式不正确

     

MySqlCommand komut = new MySqlCommand(" insert into mydb.malkontrol(tarih,birimtur,harcanan,kalan,santiye_idsantiye,malzemeler_idmalzemeler)VALUES('" + dateTimePicker1.Text +" ','" + degiscekLabel.Text +"','" + textBox3.Text +"',& #39;" + textBox4.Text +"','" + Convert.ToUInt32(comboBox1.Text)+"',&# 39;" + Convert.ToUInt32(comboBox2.Text)+"')",baglanti);

我认为2个价值成员正在发生冲突。一些知情的程序员可以解释我的错吗?

1 个答案:

答案 0 :(得分:0)

检查datetimepicker日期格式,如果数据库数据类型是datetime,则将其插入为datatimepicker1.value.tostring(" yyyy-MM-dd")。还为什么需要转换组合框值。尝试插入combobox.selectedvalue.that将插入组合框所选项目的ID。因为如果combox box值是一个字符串,它就不会像你想要的那样转换为uint32。