Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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();
      
        }
Posted
Updated 22-Mar-13 1:44am
v3
Comments
[no name] 22-Mar-13 7:45am    
Maybe because you have an ID parameter in your query but you define it as @ID in your parameter list.
[no name] 22-Mar-13 7:58am    
Additionally, you are not even checking the result of your update query. What exactly to you expect to databind when you do not get anything back from an ExecuteNonQuery?
navin ks 22-Mar-13 8:06am    
name and content must bind.. but nothing is binding now.
phil.o 22-Mar-13 8:07am    
And what do you expect to get exactly from an update command ?
navin ks 22-Mar-13 8:09am    
it must replace name and content fields

Try replaceing
C#
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);
 
Share this answer
 
Comments
navin ks 22-Mar-13 7:52am    
if i do it i get no errors and also NO RESULT.. it remains unchange
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.
 
Share this answer
 
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"
 
Share this answer
 
Comments
navin ks 22-Mar-13 7:53am    
@ID gets RESULT unchanged
ZurdoDev 22-Mar-13 7:58am    
So, put a breakpoint and see what is the actual SQL that gets executed.
navin ks 22-Mar-13 8:05am    
all is well till execute query. after query its null.. no errors shown.
ZurdoDev 22-Mar-13 8:10am    
What's null after query? Also, put a breakpoint on cmd.ExecuteNonQuery(); and then check the value of cmd.CommandText. What is it?
navin ks 22-Mar-13 8:14am    
value of cmd.command text is Update images set Name=@Name,Content=@Content where ID=1
REMEMBER IN MYSQL ALWAYS USE "?s" INSTEAD OF "@".
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900