Insert code Error

Mohamed Rafi N 106 Reputation points
2022-03-23T10:53:03.1+00:00

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

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,222 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,099 questions
{count} votes

3 answers

Sort by: Most helpful
  1. AgaveJoe 25,866 Reputation points
    2022-03-23T11:05:47.76+00:00

    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.


  2. AgaveJoe 25,866 Reputation points
    2022-03-23T13:19:50.307+00:00

    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://learn.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


  3. Lan Huang-MSFT 24,461 Reputation points Microsoft Vendor
    2022-03-24T06:53:16.01+00:00

    Hi @Mohamed Rafi N ,
    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://learn.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://learn.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://learn.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.

    0 comments No comments