question

MohamedRafiN-6400 avatar image
0 Votes"
MohamedRafiN-6400 asked karenpayneoregon commented

Data Inserting Through Selected Radio and checkbox button

Sir, in this code, if i click bad radio button2 and untaste checkbox2 means data inserting only radio button1 and taste checkbox1 that is good and tasty itself inserting in database sir

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");
}

dotnet-csharpwindows-forms
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

First and foremost, you need to take a step back and learn the basics of coding and working with C#. Learn to working with classes, separate user interface from data operations. Provide meaningful names for variables, learn proper validation when working with data and user input. As @AgaveJoe indicated, learn ASP.NET Core Razor Pages, you will find once the basics are understood coding is a great deal easier.

Next, listen to those who are trying to assist with your questions.

In regards to your question,

if i click bad radio button2 and untaste checkbox2 means data inserting only radio button1 and taste checkbox1 that is good and tasty itself inserting in database

Unsure what you mean by the highlighted words, please clarify.

1 Vote 1 ·

You posted the same issue several times but you are not following the recommendations. The main issue is you do not understand HTML forms or server controls. Radio buttons allow the user to select one option from several options. Usually the value is an integer that identifies a primary key in a database lookup table. It does not make a lot of sense to have two separate good/bad radio buttons. I provided a Web Forms radio button and checkbox example in one of your previous threads.

https://docs.microsoft.com/en-us/answers/questions/783820/insert-code-error.html

I think you'll find the HTML example useful as well. Keep in mind, server controls render standard HTML.

https://www.w3schools.com/tags/att_input_type_radio.asp

I also recommended the Wingtip Toys tutorial which also covers pattern and practices. I'm not sure if you went through the tutorial but it seems like you have not since you are asking the same question.

Also, please remove the non-related tags. This a Web Forms support question not ASP.NET Core. However, I recommend that you learn ASP.NET Core Razor Pages as Web Forms is not receiving new features.


0 Votes 0 ·

0 Answers