question

MohamedRafiN-6400 avatar image
0 Votes"
MohamedRafiN-6400 asked LanHuang-MSFT answered

Insert code Error

I manually filled fruits and year in combo box 1 & 2 here I am trying to Insert code it shows code error, please provide solution

if (comboBox1.Text !="" && comboBox2.Text != "" && radioButton1.Text !="" or radionbutton2.Text !="" && checkBox1.Text ! = "" or checkbox2.Text && textBox11.Text != "")
{
string connectionString;
MySqlConnection cnn;
connectionString = @"Data Source=localhost;Initial Catalog=testDB;User ID=root;Password=mysql";
cnn = new MySqlConnection(connectionString);
cnn.Open();
string fruits = comboBox1.Text;
int year = comboBox2.Text;
string quality = radioButton1.Text or radioButton2.Text;
string taste = checkBox1.Text checkBox2.Text;
comboBox1.Text = "";
comboBox2.Text = "";
radioButton1.Text = "";
radioButton2.Text = "";
checkBox1.Text = "";
checkBox2.Text = "";
textBox11.Text = "";
string query = "INSERT INTO fruits VALUES(@fruitsid, @fruitsname, @fruitsyear, @quality, @taste, @sales)";
using (MySqlCommand cmd = new MySqlCommand(query))
{
cmd.Parameters.AddWithValue("@fruitsid", id);
cmd.Parameters.AddWithValue("@fruitsname", name);
cmd.Parameters.AddWithValue("@fruitsyear", year);
cmd.Parameters.AddWithValue("@quality", year);
cmd.Parameters.AddWithValue("@taste", year);
cmd.Parameters.AddWithValue("@sales", year);
//comboBox1.Items.Add(ItemArray[1].ToString());
cmd.Connection = cnn;
cmd.ExecuteNonQuery();
MessageBox.Show("Record Inserted Successfully");
cnn.Close();
}
}
else
{
MessageBox.Show("Please Fill Data");
}

dotnet-csharpdotnet-aspnet-generaldotnet-aspnet-webformsdotnet-aspnet-webpages
· 5
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.

I am trying to Insert code it shows code error

And which detailed error message do you get; we can't guess that?
0 Votes 0 ·

Syntax error on if (comboBox1.Text !="" && comboBox2.Text != "" && radioButton1.Text !="" or radionbutton2.Text !="" && checkBox1.Text ! = "" or checkbox2.Text && textBox11.Text != "")

string fruits = comboBox1.Text;
int year = comboBox2.Text;
string quality = radioButton1.Text or radioButton2.Text;
string taste = checkBox1.Text checkBox2.Text;
comboBox1.Text = "";
comboBox2.Text = "";
radioButton1.Text = "";
radioButton2.Text = "";
checkBox1.Text = "";
checkBox2.Text = "";
textBox11.Text = "";
string query = "INSERT INTO fruits VALUES(@fruitsid, @fruitsname, @fruitsyear, @quality, @taste, @sales)";
using (MySqlCommand cmd = new MySqlCommand(query))
{
cmd.Parameters.AddWithValue("@fruitsid", id);
cmd.Parameters.AddWithValue("@fruitsname", name);
cmd.Parameters.AddWithValue("@fruitsyear", year);
cmd.Parameters.AddWithValue("@quality", year);
cmd.Parameters.AddWithValue("@taste", year);
cmd.Parameters.AddWithValue("@sales", year);

These are inserting condition code error

0 Votes 0 ·
Sowmitri avatar image Sowmitri MohamedRafiN-6400 ·

Syntax error
radioButton1.Text !="" or radionbutton2.Text !=""

replace



 radioButton1.Text !="" || radionbutton2.Text !=""

please check and let us know.








0 Votes 0 ·
Show more comments
AgaveJoe avatar image
0 Votes"
AgaveJoe answered AgaveJoe edited

You did not tell us the error message and the code is very sloppy. There is no indication where "name" or "Id" are set.

 cmd.Parameters.AddWithValue("@fruitsname", name);

The year value is assigned to four different parameters.

 cmd.Parameters.AddWithValue("@fruitsyear", year);
 cmd.Parameters.AddWithValue("@quality", year);
 cmd.Parameters.AddWithValue("@taste", year);
 cmd.Parameters.AddWithValue("@sales", year);

I think you need to clean up your code and do some basic debugging before posting.

· 5
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.

Sorry sir i changed it,

cmd.Parameters.AddWithValue("@fruitsyear", year);
cmd.Parameters.AddWithValue("@quality", quality);
cmd.Parameters.AddWithValue("@taste", taste);
cmd.Parameters.AddWithValue("@sales", sales);

my code full error i need to be correct it i don't know the code to correct pls guide me

0 Votes 0 ·
AgaveJoe avatar image AgaveJoe MohamedRafiN-6400 ·

You still have not shared the actual error. Even with the updated code you still have not shown where "Id", "name", or "sales" are set. Understand that we can only see the information you post on this forum.

The easiest way forward is to use the Visual Studio debugger to set a break point and single step through your code. You, the developer, should verify that each line of code functions as you intended when you wrote the code.

Navigate through code by using the Visual Studio debugger

I think it is worth mentioning that Web Forms is an older framework (20 years) and it is not moving forward. All new web application development is in ASP.NET Core. Obviously you are new to programming and I recommend that you learn the latest technology.


0 Votes 0 ·

These 3 line shows syntax error

int year = comboBox2.Text;
string quality = radioButton1.Text or radioButton2.Text;
string taste = checkBox1.Text or checkBox2.Text;

and Syntax error for if condition also
if (comboBox1.Text !="" && comboBox2.Text != "" && radioButton1.Text !="" || radioButton2.Text !="" && checkBox1.Text ! = "" || checkbox2.Text !="" && textBox11.Text != "")

0 Votes 0 ·
Show more comments
AgaveJoe avatar image
0 Votes"
AgaveJoe answered MohamedRafiN-6400 commented

i am new to develop the pgm

Everyone is new in the beginning. Most new developers go through a few tutorials rather that posting code that makes no logical sense expecting a community to figure out what you're trying to do. From my perspective you don't understand HTML forms or how server controls work. This type of information is covered in every beginning level tutorial.

Example...

186115-capture.png

Code behind

 protected void Button1_Click(object sender, EventArgs e)
 {
     string GoodBadSelected = GoodBad.SelectedValue;
     bool TastyIsChecked = Tasty.Checked;
    
     Label1.Text = $"GoodBadSelected: {GoodBadSelected} TastyIsChecked: {TastyIsChecked}";
 }

Please take my advice and go through the WingTip tutorial in my previous post.

https://docs.microsoft.com/en-us/aspnet/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/introduction-and-overview

The source code has moved to GitHub

https://github.com/Project6/Wingtip-Toys




capture.png (20.0 KiB)
· 1
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.

I don't understand mam. pls send my corrected code for my better understanding

0 Votes 0 ·
LanHuang-MSFT avatar image
0 Votes"
LanHuang-MSFT answered

Hi @MohamedRafiN-6400,
Your main mistake is not figuring out the data type.
radioButton and checkBox1 are bool types and need to use true or false literals to initialize bool variables or pass bool values.
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/bool
You need to decide whether to choose.

  if (comboBox1.Text != "" && comboBox1.Text != "" && textBox1.Text != "" && (checkBox1.Checked || checkBox2.Checked) && (radioButton1.Checked || radioButton2.Checked))

  bool quality = radioButton1.Checked || radioButton2.Checked;
  bool taste = checkBox1.Checked || checkBox2.Checked;

comboBox1.Text is of type string and cannot be directly equal to type int (mentioned in the previous question), it needs to be converted using Convert.ToInt32 Method.
https://docs.microsoft.com/en-us/dotnet/api/system.convert.toint32?view=net-6.0

  int year = Convert.ToInt32(comboBox2.Text);

I recommend you to read more basic documentation to write better code.
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide
Best regards,
Lan Huang


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


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.