MySQL Update Query not working and not updating records

Last Reply on Apr 07, 2014 12:58 AM By vail

Posted on Apr 06, 2014 03:01 AM

I have a table

stu_id,stu_name,stu_age the first stu_id is auto increment column ..

1 nnnnnn 25

2 sssssss 23

3 vail       22

  Code:

OleDbCommand cmd = new OleDbCommand("update bannerdetails set bannername='" + textslidename.Text + "',bannerimage='" + upload.PostedFile.FileName + "',bannerlink='" + textslideurl.Text + "' where id=" + userid + "", conn);

now i want to update the ssssss for 4 ...if u anty idea pls give me..

Posted on Apr 06, 2014 11:03 PM

You need to use Parameterized queries so that SQL Queries work properly

protected void Save(object sender, EventArgs e)
{
    // i have passed hard coded value here
    int id = 2;
    if (this.FileUpload1.HasFile)
    {
        string constr = ConfigurationManager.ConnectionStrings["ConString2"].ConnectionString;
        string sqlStatment = "update bannerdetails set bannername = @BannerName, bannerimage = @Image, bannerlink = @BannerLink where id = @Id";
        using (OleDbConnection con = new OleDbConnection(constr))
        {
            using (OleDbCommand cmd = new OleDbCommand(sqlStatment, con))
            {

                con.Open();
                cmd.Parameters.AddWithValue("@BannerName", this.txtBannerName.Text.Trim());
                cmd.Parameters.AddWithValue("@Image", this.FileUpload1.PostedFile.FileName);
                cmd.Parameters.AddWithValue("@BannerLink", txtBannerLink.Text);
                cmd.Parameters.AddWithValue("@Id", id);
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }
    }
}

Thank You.