parz
parz

Reputation: 19

How to submit all MySQL command only by one time Clicking on Button Event in C# Winforms

enter image description hereenter image description hereenter image description here

Dear folks,

  1. btnSubmit_Click is not able to process the code and showing error in MySql Syntax Error.
  2. I just wanted to insert the textbox input into the database at the same time select Cus_ID from database and reinsert into Orders table.
  3. In the end show the order ID
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace Project
{
    public partial class CoffeeMania : Form
    {
        public CoffeeMania()
        {
            InitializeComponent();
        }

        private void CoffeeMania_Load(object sender, EventArgs e)
        {
            cmbBranchArea.Items.Add("PJ");
            cmbBranchArea.Items.Add("KL");

            cmbCusGender.Items.Add("Male");
            cmbCusGender.Items.Add("Female");
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            string dburl = System.Configuration.ConfigurationManager.ConnectionStrings["dburl"].ConnectionString;
            MySqlConnection conn = new MySqlConnection(dburl);
            
            int qty = int.Parse(txtAmericano.Text + txtCappuccino.Text + txtMocha.Text + txtEspresso.Text + txtLatte.Text);

            string sql = "INSERT INTO customer(Cus_Name, Cus_Gender, Cus_Phone) VALUES('"+txtCusName+"', '"+cmbCusGender.SelectedItem+"', '"+txtCusPhone.Text+"';)";

            string sql2 = "select Cus_ID from customer where Cus_Name='" + txtCusName.Text + "' and Cus_Gender='" + cmbCusGender.SelectedItem + "' and Cus_Phone='" + txtCusPhone.Text + "'; ";

            try
            {
                
                conn.Open();
                MySqlCommand comm = new MySqlCommand(sql, conn);
                int record = Convert.ToInt32(comm.ExecuteNonQuery());
                Console.WriteLine(record);
                
                MySqlCommand comm2 = new MySqlCommand(sql2, conn);
                MySqlDataReader reader2 = comm2.ExecuteReader();
                if(reader2.Read())
                {
                    int CusID = Convert.ToInt32(reader2["Cus_ID"]);

                    string sql3 = "INSERT INTO orders(Orders_Qty, Cus_ID, Branch_ID) VALUES('" + qty + "', '" + CusID + "', '" + cmbBranchArea.SelectedItem + "';)";

                    MySqlCommand comm3 = new MySqlCommand(sql3, conn);
                    int record3 = Convert.ToInt32(comm3.ExecuteNonQuery());
                    Console.WriteLine(record3);

                    string sql4 = "select Orders_ID from orders where Cus_ID='" + CusID + "';";
                    MySqlCommand comm4 = new MySqlCommand(sql4, conn);
                    MySqlDataReader reader4 = comm4.ExecuteReader();
                    if(reader4.Read())
                    {
                        MessageBox.Show("Your Order ID is: " + reader4["Orders_ID"]);
                    }
                    
                }
                
                lblShowVoteResult.Text = "Successfully submitted";

                comm.Dispose();
                conn.Close();
            }

            catch(MySqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

Upvotes: 0

Views: 43

Answers (1)

Saiko
Saiko

Reputation: 112

You get the Syntaxerror because you insert the semicolon before your ")".

For example change:

string sql3 = "INSERT INTO orders(Orders_Qty, Cus_ID, Branch_ID) VALUES('" + qty + "', '" + CusID + "', '" + cmbBranchArea.SelectedItem + "';)";

// extra semicolon here ^

to

string sql3 = "INSERT INTO orders(Orders_Qty, Cus_ID, Branch_ID) VALUES('" + qty + "', '" + CusID + "', '" + cmbBranchArea.SelectedItem + "');";

and everywhere else.

Upvotes: 1

Related Questions