private void btnUpdate_Click(object sender, EventArgs e) { try { if (ID > -1) { dbconnection.Close(); dbconnection.Open(); string query = "update car_partner_income set Car_ID ='" + comCarNumber.SelectedValue + "' ,ClientName='" + txtAddress.Text + "',NoCarton='" + txtNoCarton.Text + "',NoSets=" + txtNoSets.Text + ",NoDocks=" + txtNoDocks.Text + ",NoColumns=" + txtNoColumns.Text + ",NoCompinations=" + txtNoComp.Text + ",NoPanio=" + txtNoPanio.Text + " where CarPartner_Income_ID=" + ID + ""; MySqlCommand com = new MySqlCommand(query, dbconnection); com.ExecuteNonQuery(); query = "delete from car_partner_permission where CarPartner_Income_ID=" + ID + ""; com = new MySqlCommand(query, dbconnection); com.ExecuteNonQuery(); foreach (DataGridViewRow row in dataGridView1.Rows) { if (ID > 0) { query = "insert into car_partner_permission (CarPartner_Income_ID,Permission_Number) values (@CarPartner_Income_ID,@Permission_Number)"; com = new MySqlCommand(query, dbconnection); com.Parameters.Add("@CarPartner_Income_ID", MySqlDbType.Int16); com.Parameters["@CarPartner_Income_ID"].Value = ID; com.Parameters.Add("@Permission_Number", MySqlDbType.VarChar); com.Parameters["@Permission_Number"].Value = row.Cells[0].Value; com.ExecuteNonQuery(); } } MessageBox.Show("Done"); carIncomes.displayData(); } else { MessageBox.Show("select row please"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void btnAdd_Click(object sender, EventArgs e) { try { dbconnection.Open(); if (txtAddress.Text != "") { string query = "insert into car_partner_income (Car_ID,ClientName,NoCarton,NoSets,NoDocks,NoColumns,NoCompinations,NoPanio,Date,Note) values (@Car_ID,@ClientName,@NoCarton,@NoSets,@NoDocks,@NoColumns,@NoCompinations,@NoPanio,@Date,@Note)"; MySqlCommand com = new MySqlCommand(query, dbconnection); com.Parameters.Add("@Date", MySqlDbType.Date); com.Parameters["@Date"].Value = dateTimePicker1.Value.Date; com.Parameters.Add("@Car_ID", MySqlDbType.Int16); com.Parameters["@Car_ID"].Value = comCarNumber.SelectedValue; com.Parameters.Add("@ClientName", MySqlDbType.VarChar); com.Parameters["@ClientName"].Value = txtAddress.Text; com.Parameters.Add("@NoCarton", MySqlDbType.Double); com.Parameters["@NoCarton"].Value = txtNoCarton.Text; com.Parameters.Add("@NoSets", MySqlDbType.Double); com.Parameters["@NoSets"].Value = txtNoSets.Text; com.Parameters.Add("@NoDocks", MySqlDbType.Double); com.Parameters["@NoDocks"].Value = txtNoDocks.Text; com.Parameters.Add("@NoColumns", MySqlDbType.Double); com.Parameters["@NoColumns"].Value = txtNoColumns.Text; com.Parameters.Add("@NoCompinations", MySqlDbType.Double); com.Parameters["@NoCompinations"].Value = txtNoComp.Text; com.Parameters.Add("@NoPanio", MySqlDbType.Double); com.Parameters["@NoPanio"].Value = txtNoPanio.Text; if (txtNote.Text != "") { com.Parameters.Add("@Note", MySqlDbType.VarChar); com.Parameters["@Note"].Value = txtNote.Text; } else { com.Parameters.Add("@Note", MySqlDbType.VarChar); com.Parameters["@Note"].Value = null; } com.ExecuteNonQuery(); query = "select CarPartner_Income_ID from car_partner_income order by CarPartner_Income_ID Desc limit 1"; com = new MySqlCommand(query, dbconnection); int id = 0; if (com.ExecuteScalar() != null) { id = Convert.ToInt16(com.ExecuteScalar()); } else { MessageBox.Show("error :("); dbconnection.Close(); return; } foreach (DataGridViewRow row in dataGridView1.Rows) { if (id > 0) { query = "insert into car_partner_permission (CarPartner_Income_ID,Permission_Number) values (@CarPartner_Income_ID,@Permission_Number)"; com = new MySqlCommand(query, dbconnection); com.Parameters.Add("@CarPartner_Income_ID", MySqlDbType.Int16); com.Parameters["@CarPartner_Income_ID"].Value = id; com.Parameters.Add("@Permission_Number", MySqlDbType.VarChar); com.Parameters["@Permission_Number"].Value = row.Cells[0].Value; com.ExecuteNonQuery(); } } MessageBox.Show("ADD Success"); carIncomes.displayData(); clear(); } else { MessageBox.Show("insert data to all fields"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } dbconnection.Close(); }