C# SQL Query Syntax Problem

HaKDMoDz™ 1 Reputation point
2021-09-17T22:38:34.507+00:00

Hi I have a sql query syntax error near 150.00 the error states If someone could have the time to help me fix it please.


private void AddPayment_Button_Click(object sender, EventArgs e)
        {
            string insertQuery = "INSERT INTO Debt_Payment_Tracker.payments(payments_id,payments_amount,payments_date,payments_total)VALUES('"+recNum+ "," + Convert.ToDouble(valuePicker_CB.SelectedItem) + "," + dateUS + "," + 150.00;"'));

            MySqlCommand comand = new MySqlCommand(insertQuery, Connection);
            try
            {
                if (comand.ExecuteNonQuery() == 1)
                {
                    MessageBox.Show("Data Inserted");
                }
                else
                {
                    MessageBox.Show("Data Not Inserted !");
                }
                Connection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                throw;
            }
            Debt_Payment_Tracker.payments;";
        }
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,484 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,098 questions
{count} votes

1 answer

Sort by: Most helpful
  1. P a u l 10,401 Reputation points
    2021-09-17T22:50:52.547+00:00

    This should work:

    string insertQuery = "INSERT INTO Debt_Payment_Tracker.payments(payments_id,payments_amount,payments_date,payments_total) VALUES (@payments_id, @payments_amount, @payments_date, @payments_total);";
    
    MySqlCommand comand = new MySqlCommand(insertQuery, Connection);
    
    command.Parameters.AddWithValue("@payments_id", recNum);
    command.Parameters.AddWithValue("@payments_amount", Convert.ToDouble(valuePicker_CB.SelectedItem));
    command.Parameters.AddWithValue("@payments_date", dateUS);
    command.Parameters.AddWithValue("@payments_total", 150.00);
    
    try {
    if (comand.ExecuteNonQuery() == 1) {
    MessageBox.Show("Data Inserted");
    } else {
    MessageBox.Show("Data Not Inserted !");
    }
    Connection.Close();
    } catch (Exception ex) {
    MessageBox.Show(ex.Message);
    throw;
    }
    

    You had this on the last line and it looks like a fragment of another query - I'd remove it because it'll cause a C# syntax error:

    Debt_Payment_Tracker.payments;";
    
    0 comments No comments