Before you comment please note that I understand that my code is vulnerable to SQL injection, please disregard any comments about it being vulnerable for purposes of simplicity
I've checked around the website for answers but none seem to fit my situation, many are PHP.
I am trying to update information on a MySQL database from C# Forms Application on Visual Studio 2012, so I've allowed the user to input data but I want them to be able to update their data.
I've tried all sorts of different methods many give me errors, I feel like I'm very close with this method.
string Connection = "server = localhost; " + "database = root; " + "uid = root;" + "pwd = password;";
private void btnSubmit_Click(object sender, EventArgs e)
{
MySqlConnection Conn = new MySqlConnection(Connection);
try
{
Conn.Open();
string Query = "Update users (Calories) VALUES(@Calories) WHERE username = " + Form1.sName + "AND Day = " + Form1.iDay;
MySqlCommand cmd = new MySqlCommand(Query, Conn);
cmd.Parameters.AddWithValue("@Calories", txtCalories.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Data Saved");
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
Conn.Close();
}
It is telling me there is an error with my syntax.
I've allowed the user to enter information on the previous form, after clicking continue it passes the data to the next form which is this form.
I want them to be able to update their data on the database with information that they enter on this one BUT only for the most recent day which is why I've included the day variable.
Form1.sNameat least. This would have been handled if you had made it a parameter instead of using string concatenation.update users set Calories = @Calories where ....cmd.Parameters.AddWithValue()is the best way and should be used for all variables going into your query.