Mohamed Rafi

Mohamed Rafi

  • NA
  • 74
  • 9.1k

Insert Through Radio button and Checkbox button

Mar 26 2022 9:59 AM

My insert code is not working fine, bcz if i click radionbutton2 and checkbox 2 means it was inserting data automatically radionbutton1 and checkbox1 only, actually if which textbox is selected that data should enter null value radionbutton data should not saved in db

if (!string.IsNullOrEmpty(textBox11.Text) && !string.IsNullOrEmpty(textBox12.Text) && !string.IsNullOrEmpty(textBox13.Text) && !string.IsNullOrEmpty(textBox14.Text) && (radioButton1.Checked || radioButton2.Checked) && (checkBox1.Checked || checkBox2.Checked))
            {
                string connectionString;
                MySqlConnection cnn;
                connectionString = @"Data Source=localhost;Initial Catalog=testDB;User ID=root;Password=mysql";
                cnn = new MySqlConnection(connectionString);
                cnn.Open();
                string id = textBox14.Text;
                string name = textBox11.Text;
                string year = textBox12.Text;
                string quality = radioButton1.Text == "" ? radioButton2.Text : radioButton1.Text;
                string taste = checkBox1.Text == "" ? checkBox2.Text : checkBox1.Text;
                string sales = textBox13.Text;
                textBox11.Text = "";
                textBox12.Text = "";
                textBox13.Text = "";
                textBox14.Text = "";
                radioButton1.Text = "";
                radioButton2.Text = "";
                checkBox1.Text = "";
                checkBox2.Text = "";
                string query = "INSERT INTO fruits VALUES(@fruitsid, @fruitsname, @fruitsyear, @quality, @taste, @sales)";
                using (MySqlCommand cmd = new MySqlCommand(query))
                {
                    cmd.Parameters.AddWithValue("@fruitsid", Convert.ToInt32(id));
                    cmd.Parameters.AddWithValue("@fruitsname", name);
                    cmd.Parameters.AddWithValue("@fruitsyear", year);
                    cmd.Parameters.AddWithValue("@quality", quality);
                    cmd.Parameters.AddWithValue("@taste", taste);
                    cmd.Parameters.AddWithValue("@sales", sales);
                    cmd.Connection = cnn;
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Record Inserted Successfully");
                    DisplayData();
                    cnn.Close();
                }
            }
            else
            {
                MessageBox.Show("Please Fill Data");
            }


Answers (3)