question

HaKDMoDz avatar image
0 Votes"
HaKDMoDz asked JackJJun-MSFT commented

C# SQL Query Syntax Problem

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;";
         }
dotnet-csharpdotnet-sqlclient
· 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.

@HaKDMoDz, have you tried the solution by Paul-5034? Please feel free to let know if you have any question.

0 Votes 0 ·

1 Answer

Paul-5034 avatar image
0 Votes"
Paul-5034 answered Paul-5034 edited

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

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.