Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Inserting Through Radionbutton and checkbox

Inserting Through Radionbutton and checkbox

Scheduled Pinned Locked Moved C#
databasemysqlcomsalesquestion
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    N Mohamed rafi
    wrote on last edited by
    #1

    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();

    OriginalGriffO L C 3 Replies Last reply
    0
    • N N Mohamed rafi

      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();

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Quote:

      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

      The question is meaningless, you still haven't learned how to use a debugger, and you are still trying to get others to fix problems instead of thinking about what is actually going on. what have you tried to work out what the problem might be, given that we can't understand your "explanation" of the problem, run your code, see your inputs, or access your DB?

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • N N Mohamed rafi

        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();

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        And you are still posting a success message even if it fails.

        cmd.ExecuteNonQuery();
        MessageBox.Show("Record Inserted Successfully");

        L 1 Reply Last reply
        0
        • N N Mohamed rafi

          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();

          C Offline
          C Offline
          Craig Robbins
          wrote on last edited by
          #4

          I'm sorry but i don't understand your question. One thought I have - are you trying to do an 'exclusive or'? In other words, are you trying to see if button 1 is selected, or button 2 is selected, but not both at the same time? As others have pointed out a few different times in response to your questions - you cannot assume a SQL command is successful just because control moves to the next statement. You need to check to see if you were able to perform the SQL statement. You may lose your connection, you may not be authorized to the database, the update may have failed to run. There are ways to see if the SQL statement worked before you proceed to posting a 'success' message.

          N 1 Reply Last reply
          0
          • C Craig Robbins

            I'm sorry but i don't understand your question. One thought I have - are you trying to do an 'exclusive or'? In other words, are you trying to see if button 1 is selected, or button 2 is selected, but not both at the same time? As others have pointed out a few different times in response to your questions - you cannot assume a SQL command is successful just because control moves to the next statement. You need to check to see if you were able to perform the SQL statement. You may lose your connection, you may not be authorized to the database, the update may have failed to run. There are ways to see if the SQL statement worked before you proceed to posting a 'success' message.

            N Offline
            N Offline
            N Mohamed rafi
            wrote on last edited by
            #5

            yes sir need to select any one at a time. and i am using mysql not sql

            C 1 Reply Last reply
            0
            • L Lost User

              And you are still posting a success message even if it fails.

              cmd.ExecuteNonQuery();
              MessageBox.Show("Record Inserted Successfully");

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              Optimism is a moral duty... :-D

              Luc Pattyn [My Articles] The Windows 11 "taskbar" is disgusting. It should be at the left of the screen, with real icons, with text, progress, etc. They downgraded my developer PC to a bloody iPhone.

              1 Reply Last reply
              0
              • N N Mohamed rafi

                yes sir need to select any one at a time. and i am using mysql not sql

                C Offline
                C Offline
                Craig Robbins
                wrote on last edited by
                #7

                You will need to research how to do an "exclusive or" to learn the syntax. As for my SQL comment - it still applies to MySQL. You must learn how to tell if the SQL command ran successfully. You cannot assume that it ran as desired just because control flows to the next statement. This is why others are telling you to check for sucess on your SQL operation before you proceed to the statement that says it succeeded. When you become a professional programmer, you need to worry about things like "Do I have authority to access the database? Is the database online? Did some other unexpected condition occur?" Much of programming is dealing with gracefully handling errors and unexpected conditions. CodeProject members are professionals and their tips and suggestions are to help you achieve that skill level.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups