I'm trying to update a record in C# using MySQL. It fails when I run it in visual studio but works when I get the raw SQL and run it in phpadmin. Can you see where I'm going wrong?
Here's the code:
string query = "UPDATE `tblSections` SET `Heading` = @Heading, `Content` = @Contents, `DateAdded` = @DateAdded, `Intro` = @Intro, `UserID` = @UserID, `Deleted`, = @Deleted, `Archived` = @Archived, `AURL` = @AURL, `Tags` = @Tags, `ImageID` = @ImageID, `PostID` = @PostID, `Section` = @Section, `HeaderImage` = @HeaderImage WHERE ID = @ID";
string conString = ConfigurationManager.AppSettings["DBConnectMySql"];
using (MySqlConnection con = new MySqlConnection(conString))
{
using (MySqlCommand cmd = new MySqlCommand(query, con))
{
string checkImage = selectImagehidden.Value;
int iID = 0;
if (checkImage == "0")
{
//keep default
}
else {
iID = Int32.Parse(selectImagehidden.Value);
};
cmd.Parameters.AddWithValue("@ID", txtID.Text);
cmd.Parameters.AddWithValue("@Heading", txtTitle.Text);
cmd.Parameters.AddWithValue("@Contents", HttpUtility.HtmlEncode(txtBody.Text));
cmd.Parameters.AddWithValue("@DateAdded", DateTime.Now);
cmd.Parameters.AddWithValue("@Intro", HttpUtility.HtmlEncode(txtIntro.Text));
cmd.Parameters.AddWithValue("@UserID", (int)Session["MDJ_UserID"]);
cmd.Parameters.AddWithValue("@Deleted", 0);
cmd.Parameters.AddWithValue("@Archived", 0);
cmd.Parameters.AddWithValue("@AURL", txtURL.Text);
cmd.Parameters.AddWithValue("@Tags", txtTags.Text);
cmd.Parameters.AddWithValue("@ImageID", iID);
cmd.Parameters.AddWithValue("@PostID", 0);
cmd.Parameters.AddWithValue("@Section", headertext.Text);
cmd.Parameters.AddWithValue("@HeaderImage", txtHeaderImage.Text);
con.Open();
And the error:
MySql.Data.MySqlClient.MySqlException (0x80004005): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' = 0, Archived = 0, AURL = 'top', Tags = 'top, heading, content', ImageID = 0, P' at line 1 at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
I've used this to spit out the raw SQL for testing:
string querycheck = cmd.CommandText;
foreach (MySqlParameter p in cmd.Parameters)
{
querycheck = querycheck.Replace(p.ParameterName, p.Value.ToString());
}
Any ideas what i'm doing wrong? Thanks
User contributions licensed under CC BY-SA 3.0