protected void GVImages_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string ID = ((Label)GVImages.Rows[e.RowIndex].FindControl("lblID")).Text;
string Name = ((TextBox)GVImages.Rows[e.RowIndex].FindControl("txtName")).Text;
FileUpload f = ((FileUpload)GVImages.Rows[e.RowIndex].FindControl("FileUpload2"));
byte[] imageBytes = null;
if (f.HasFile)
{
//variable to store the image content
imageBytes = new byte[f.PostedFile.ContentLength];
HttpPostedFile uploadImage = f.PostedFile;
//read the image stream from the post and store it in imageBytes
uploadImage.InputStream.Read(imageBytes, 0, (int)f.PostedFile.ContentLength);
}
con.Open();
MySqlCommand cmd = new MySqlCommand("Update images set Name=@Name,Content=@Content where ID="+ID, con);
cmd.Parameters.Add("@ID", MySqlDbType.Int16).Value = ID;
cmd.Parameters.Add("@Name", MySqlDbType.VarChar).Value= Name;
cmd.Parameters.Add("@Content", MySqlDbType.LongBlob).Value = imageBytes;
cmd.Connection = con;
cmd.ExecuteNonQuery();
GVImages.EditIndex = -1;
con.Close();
databind();
}
解决方案Try replaceing
MySqlCommand cmd = new MySqlCommand("Update images set Name=@Name,Content=@Content where ID=" +ID, con);
with
MySqlCommand cmd = new MySqlCommand(" Update images set Name=@Name,Content=@Content where ID=@ID", con);
Basically, ExecuteNonQuery() method should return you an integer, but you don''t catch it in any variable in your code ; you can watch its value by debugging, either.
The question you should concentrate on is : What do you want to get from an UPDATE command ?
Do you expect to get a resultset (i.e., one or more rows) ? You won''t.
If you want to get a resultset from a query, it must be a SELECT one.
So, please, we need to know what exactly you are expecting from your SQL request.
As ThePhantomMenace pointed out, you are concatenating on the ID in the string but then are adding a parameter named @ID. You need to change the SQL command text to be
"Update images set Name=@Name,Content=@Content where ID=@ID"