Jun Harefa

Jun Harefa

  • NA
  • 12
  • 22.2k

[Help] How to add new data into MySql with two Tables in C#

Aug 28 2011 11:04 PM
I want to add new data to the MySql database. I have two tables: tb_bahan_baku and tb_kategori.

tb_bahan_baku contains: kode_barang , nama, kode_kategori, satuan, harga, keterangan

tb_kategori contains: kode_kategori and is a foreign-key in the tb_bahan_baku table.

My problem is, I want to add new data to the tb_bahan_baku table and then choose one data in ComboBox of the kode_kategori which contains: KTG-01, KTG-02, KTG-03. The data that I choose of the kode_kategori only an option as a category.

What should I do to add new data to the tb_bahan_baku and also set the insert the data into tb_kategori table

Here the code that I have created, but still error
private void btnSave_Click(object sender, EventArgs e)     {       string connectionSQL = "server=localhost;user id=root;password=;database=db_junisman_kulit;";       MySqlConnection conn = new MySqlConnection(connectionSQL);         try       {         MySqlCommand cmd = new MySqlCommand("INSERT INTO tb_bahan_baku(kode_barang,nama,kode_kategori,satuan,harga,keterangan) VALUES ('" + textBox1.Text + "', '" + textBox2.Text + "', '" + comboBox2.SelectedValue + "', '" + comboBox2.SelectedValue + "', '" + textBox3.Text + "', '" + richTextBox1.Text + "')", conn);         conn.Open();         cmd.ExecuteNonQuery();         MessageBox.Show("Data Telah disimpan!!");       }        catch (MySqlException ex)       {          MessageBox.Show("Can't connect to database\n" + ex.ToString());       }       finally       {         conn.Close();         MessageBox.Show("Terima Kasih - Junisman Harefa");       }     }


Thanks in advance..