private void btnAddProduct_Click(object sender, EventArgs e) { try { if (dataGridView2.Rows.Count > 0) { if (txtSetName.Text != "") { string query = "select Set_Name from sets where Set_Name='" + txtSetName.Text + "'"; MySqlCommand comand = new MySqlCommand(query, dbconnection); dbconnection.Open(); MySqlDataReader dr = comand.ExecuteReader(); while (dr.Read()) { if (dr["Set_Name"].ToString() == txtSetName.Text) { MessageBox.Show("this set already exist"); dr.Close(); dbconnection.Close(); return; } } dr.Close(); if (selectedImage == null) { query = "INSERT INTO sets (Set_Name,Factory_ID,Type_ID,Group_ID) VALUES (@Set_Name,@Factory_ID,@Type_ID,@Group_ID)"; comand = new MySqlCommand(query, dbconnection); comand.Parameters.AddWithValue("@Set_Name", txtSetName.Text); string q = "select Type_ID from type where Type_Name='" + dataGridView2.Rows[0].Cells[3].Value.ToString() + "'"; MySqlCommand com = new MySqlCommand(q, dbconnection); comand.Parameters.Add("@Type_ID", MySqlDbType.Int16); comand.Parameters["@Type_ID"].Value = com.ExecuteScalar(); q = "select Factory_ID from factory where Factory_Name='" + dataGridView2.Rows[0].Cells[4].Value.ToString() + "'"; com = new MySqlCommand(q, dbconnection); comand.Parameters.Add("@Factory_ID", MySqlDbType.Int16); comand.Parameters["@Factory_ID"].Value = com.ExecuteScalar(); q = "select Group_ID from groupo where Group_Name='" + dataGridView2.Rows[0].Cells[5].Value.ToString() + "'"; com = new MySqlCommand(q, dbconnection); comand.Parameters.Add("@Group_ID", MySqlDbType.Int16); comand.Parameters["@Group_ID"].Value = com.ExecuteScalar(); comand.ExecuteNonQuery(); } else { query = "INSERT INTO sets (Set_Name,Factory_ID,Type_ID,Group_ID,Set_Photo) VALUES (@Set_Name,@Factory_ID,@Type_ID,@Group_ID,@Set_Photo)"; comand = new MySqlCommand(query, dbconnection); comand.Parameters.AddWithValue("@Set_Name", txtSetName.Text); string q = "select Type_ID from type where Type_Name='" + dataGridView2.Rows[0].Cells[3].Value.ToString() + "'"; MySqlCommand com = new MySqlCommand(q, dbconnection); comand.Parameters.Add("@Type_ID", MySqlDbType.Int16); comand.Parameters["@Type_ID"].Value = com.ExecuteScalar(); q = "select Factory_ID from factory where Factory_Name='" + dataGridView2.Rows[0].Cells[4].Value.ToString() + "'"; com = new MySqlCommand(q, dbconnection); comand.Parameters.Add("@Factory_ID", MySqlDbType.Int16); comand.Parameters["@Factory_ID"].Value = com.ExecuteScalar(); q = "select Group_ID from groupo where Group_Name='" + dataGridView2.Rows[0].Cells[5].Value.ToString() + "'"; com = new MySqlCommand(q, dbconnection); comand.Parameters.Add("@Group_ID", MySqlDbType.Int16); comand.Parameters["@Group_ID"].Value = com.ExecuteScalar(); comand.Parameters.Add("@Set_Photo", MySqlDbType.LongBlob); comand.Parameters["@Set_Photo"].Value = selectedImage; comand.ExecuteNonQuery(); } query = "select Set_ID from sets order by Set_ID desc limit 1"; comand = new MySqlCommand(query, dbconnection); int set_id = Convert.ToInt16(comand.ExecuteScalar().ToString()); foreach (DataGridViewRow item in dataGridView2.Rows) { query = "INSERT INTO set_Details (Set_ID,Data_ID,Quantity) VALUES (@Set_ID,@Data_ID,@Quantity)"; comand = new MySqlCommand(query, dbconnection); comand.Parameters.AddWithValue("@Set_ID", set_id); comand.Parameters.AddWithValue("@Data_ID", item.Cells[0].Value); comand.Parameters.AddWithValue("@Quantity", double.Parse(item.Cells[2].Value.ToString())); comand.ExecuteNonQuery(); } //UserControl.UserRecord("sets", "add", set_id.ToString(), DateTime.Now, dbconnection); MessageBox.Show("Done"); clear(); ataqm.DisplayAtaqm(); } else { MessageBox.Show("Please fill all fields with right format"); } } else { MessageBox.Show("Please insert at least one item"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } dbconnection.Close(); }
private void btnUpdateSet_Click(object sender, EventArgs e) { try { dbconnection.Open(); deleteSet(Convert.ToInt16(updateRow[0].ToString())); if (dataGridView2.Rows.Count > 0) { if (txtSetName.Text != "") { if (ImageProduct.Image == null) { String query = "update sets set Set_Name=@Set_Name,Type_ID=@Type_ID,Factory_ID=@Factory_ID,Group_ID=@Group_ID where Set_ID=" + updateRow[0].ToString(); MySqlCommand comand = new MySqlCommand(query, dbconnection); comand.Parameters.AddWithValue("@Set_Name", txtSetName.Text); string q = "select Type_ID from type where Type_Name='" + dataGridView2.Rows[0].Cells[3].Value.ToString() + "'"; MySqlCommand com = new MySqlCommand(q, dbconnection); comand.Parameters.Add("@Type_ID", MySqlDbType.Int16); comand.Parameters["@Type_ID"].Value = com.ExecuteScalar(); q = "select Factory_ID from factory where Factory_Name='" + dataGridView2.Rows[0].Cells[4].Value.ToString() + "'"; com = new MySqlCommand(q, dbconnection); comand.Parameters.Add("@Factory_ID", MySqlDbType.Int16); comand.Parameters["@Factory_ID"].Value = com.ExecuteScalar(); q = "select Group_ID from groupo where Group_Name='" + dataGridView2.Rows[0].Cells[5].Value.ToString() + "'"; com = new MySqlCommand(q, dbconnection); comand.Parameters.Add("@Group_ID", MySqlDbType.Int16); comand.Parameters["@Group_ID"].Value = com.ExecuteScalar(); comand.ExecuteNonQuery(); } else { String query = "update sets set Set_Name=@Set_Name ,Set_Photo=@Set_Photo where Set_ID=" + updateRow[0].ToString(); MySqlCommand comand = new MySqlCommand(query, dbconnection); comand.Parameters.AddWithValue("@Set_Name", txtSetName.Text); string q = "select Type_ID from type where Type_Name='" + dataGridView2.Rows[0].Cells[3].Value.ToString() + "'"; MySqlCommand com = new MySqlCommand(q, dbconnection); comand.Parameters.Add("@Type_ID", MySqlDbType.Int16); comand.Parameters["@Type_ID"].Value = com.ExecuteScalar(); q = "select Factory_ID from factory where Factory_Name='" + dataGridView2.Rows[0].Cells[4].Value.ToString() + "'"; com = new MySqlCommand(q, dbconnection); comand.Parameters.Add("@Factory_ID", MySqlDbType.Int16); comand.Parameters["@Factory_ID"].Value = com.ExecuteScalar(); q = "select Group_ID from groupo where Group_Name='" + dataGridView2.Rows[0].Cells[5].Value.ToString() + "'"; com = new MySqlCommand(q, dbconnection); comand.Parameters.Add("@Group_ID", MySqlDbType.Int16); comand.Parameters["@Group_ID"].Value = com.ExecuteScalar(); comand.Parameters.Add("@Set_Photo", MySqlDbType.LongBlob); comand.Parameters["@Set_Photo"].Value = selectedImage; comand.ExecuteNonQuery(); } int set_id = Convert.ToInt16(updateRow[0].ToString()); foreach (DataGridViewRow item in dataGridView2.Rows) { String query = "INSERT INTO set_Details (Set_ID,Data_ID,Quantity) VALUES (@Set_ID,@Data_ID,@Quantity)"; MySqlCommand comand = new MySqlCommand(query, dbconnection); comand.Parameters.AddWithValue("@Set_ID", set_id); comand.Parameters.AddWithValue("@Data_ID", item.Cells[0].Value); comand.Parameters.AddWithValue("@Quantity", double.Parse(item.Cells[2].Value.ToString())); comand.ExecuteNonQuery(); } // UserControl.UserRecord("sets", "update", set_id.ToString(), DateTime.Now, dbconnection); MessageBox.Show("Done"); clear(); ataqm.DisplayAtaqm(); } else { MessageBox.Show("Please fill all fields with right format"); } } else { MessageBox.Show("Please insert at least one item"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } dbconnection.Close(); }