Aktham Mahmoud

Aktham Mahmoud

  • NA
  • 720
  • 38.1k

I can not able to update one column

Aug 6 2020 3:39 PM
Greeting all
 I've faced this problem, and I don't know what the cause for it.
See table T_SQL code
 
  1. CREATE TABLE [dbo].[Categories] (  
  2.     [C_Id]          INT            IDENTITY (1, 1) NOT NULL,  
  3.     [C_Name]        NVARCHAR (50)  NULL,  
  4.     [C_Description] NVARCHAR (500) NULL,  
  5.     [C_Enable]      BIT            NULL,  
  6.     [Cat_photo]     NVARCHAR (500) NULL,  
  7.     [CPhoto_Name]   NVARCHAR (50)  NULL,  
  8.     PRIMARY KEY CLUSTERED ([C_Id] ASC)  
  9. );  
 What the problem? When I need to update a row by gridview, all colums updated without ((cat_photo)) column
 
See below GrideView Code, whch I used to update :
  1. protected void GV_Category_RowCommand(object sender, GridViewCommandEventArgs e)  
  2. {  
  3.     //Get GridView RowIndex  
  4.     GridViewRow gvr = (GridViewRow)(((Button)e.CommandSource).NamingContainer);  
  5.     int rowid = gvr.RowIndex;  
  6.     //End GridView RowIndex  
  7.     using (SqlConnection sqlconUpcat = new SqlConnection(connectionString))  
  8.   
  9.         if (e.CommandName == "Update")  
  10.         {  
  11.             try  
  12.             {  
  13.                 FileUpload FUgvCat= (FileUpload)GV_Category.Rows[rowid].FindControl("FU_CIMG_E");  
  14.                 if (FUgvCat.HasFile)  
  15.                 {  
  16.                     string FN_CatImage = FUgvCat.PostedFile.FileName;  
  17.                     FUgvCat.SaveAs(Server.MapPath("~/images/categories/" + Path.GetFileName(FN_CatImage)));  
  18.                     string photopath = "~/images/categories/" + Path.GetFileName(FN_CatImage);                
  19.                     //Open Connection with D.B       
  20.                     string Query_Up = "UPDATE Categories SET [C_Name] = @C_Name, [C_Description] = @C_Description, [C_Enable] = @C_Enable, [Cat_photo] = @Cat_photo, [CPhoto_Name]= @CPhoto_Name WHERE (C_Id = @C_Id)";  
  21.                     sqlconUpcat.Open();  
  22.                         //=============================================  
  23.                         SqlCommand sqlcmdUCat = new SqlCommand(Query_Up, sqlconUpcat);  
  24.                         sqlcmdUCat.Parameters.AddWithValue("@C_Name", (GV_Category.Rows[rowid].FindControl("TB_CNameE"as TextBox).Text);  
  25.                         sqlcmdUCat.Parameters.AddWithValue("@C_Description", (GV_Category.Rows[rowid].FindControl("TB_Cdesc"as TextBox).Text);  
  26.                         sqlcmdUCat.Parameters.AddWithValue("@C_Enable", (GV_Category.Rows[rowid].FindControl("ChBCE"as CheckBox).Checked);  
  27.                         //sqlcmdUCat.Parameters.AddWithValue("@Cat_photo",photopath);  
  28.                         sqlcmdUCat.Parameters.AddWithValue("@CPhoto_Name", (Path.GetFileName(FN_CatImage)));  
  29.                         sqlcmdUCat.Parameters.AddWithValue("@C_Id", Convert.ToInt32(GV_Category.DataKeys[rowid].Value.ToString()));  
  30.                         //=============================================  
  31.                         sqlcmdUCat.ExecuteNonQuery();  
  32.                         GV_Category.EditIndex = -1;  
  33.                        }//===>End String Query  
  34.                         lbSCatMsg.Text = "Success";   
  35.                 }//End If (File Upload has file)  
  36.             }//End Try  
  37.             catch (Exception ex)  
  38.             {  
  39.                 lbFCatMsg.Text = ex.Message;  
  40.             }  
  41.         }//===>End IF (Update)  
  42.   
  43. }          
I reusing another method, I said maybe qlcmdUCat.Parameters.AddWithValue( Can't able to catch object decleared.
 
 
  1.         protected void GV_Category_RowCommand(object sender, GridViewCommandEventArgs e)  
  2.         {  
  3.             //Get GridView RowIndex  
  4.             GridViewRow gvr = (GridViewRow)(((Button)e.CommandSource).NamingContainer);  
  5.             int rowid = gvr.RowIndex;  
  6.             //End GridView RowIndex  
  7.   
  8.             using (SqlConnection sqlconUpcat = new SqlConnection(connectionString))  
  9.   
  10.                 if (e.CommandName == "Update")  
  11.                 {  
  12.                     try  
  13.                     {  
  14.   
  15.                         FileUpload FUgvCat= (FileUpload)GV_Category.Rows[rowid].FindControl("FU_CIMG_E");  
  16.                         if (FUgvCat.HasFile)  
  17.                         {  
  18.     //upload the file onto the server     
  19.                             string FN_CatImage = FUgvCat.PostedFile.FileName;  
  20.                             FUgvCat.SaveAs(Server.MapPath("~/images/categories/" + Path.GetFileName(FN_CatImage)));  
  21.                             string photopath = "~/images/categories/" + Path.GetFileName(FN_CatImage);  
  22. //Declear variblaes  
  23.              TextBox TBName = (GV_Category.Rows[rowid].FindControl("TB_CNameE"as TextBox);  
  24.                             TextBox TBDEsc = (GV_Category.Rows[rowid].FindControl("TB_Cdesc"as TextBox);  
  25.                             CheckBox CHEn = (GV_Category.Rows[rowid].FindControl("ChBCE"as CheckBox);  
  26.                             int Catid = Convert.ToInt32(GV_Category.DataKeys[rowid].Value.ToString());  
  27.                               
  28.         //Open Connection with D.B  
  29.                             sqlconUpcat.Open();  
  30.                             {  

  31.  SqlCommand sqlcmd = new SqlCommand("UPDATE Categories SET [C_Name] ='" + TBName.Text + "', [C_Description] = '" + TBDEsc.Text + "', [C_Enable] = '" + CHEn.Checked + "', [Cat_photo] = '" + photopath + "', [CPhoto_Name]='" + (Path.GetFileName(FN_CatImage)) + "' where C_Id='" + Catid + "'", sqlcon);  
  32.                                 
  33.                                 sqlcmdUCat.ExecuteNonQuery();  
  34.                                 GV_Category.EditIndex = -1;  
  35.                                }//===>End String Query  
  36.                                 lbSCatMsg.Text = "Success"   
  37.                               
  38.                         }//End If (File Upload has file)         
  39.                     }//End Try  
  40.                     catch (Exception ex)  
  41.                     {  
  42.                         lbFCatMsg.Text = ex.Message;  
  43.                     }  
  44.                 }//===>End IF (Update)  
  45.         }          
Maybe someone will said a decleard varibale not catch any data and insert (Null) Value actually, my brother test to pass a ((photopath )) to label and the result when I update row as below.
 
When I run a project a row updated and take a photo name, put a path not inserted,see photo below 
 
 
 Another carzy solution which I made it:
-recreate table(Not help).
-Delete (Temporary ASP.NET Files ) not help too.
So anyone able to provide what a crazy bug in code.
Thanks for your help.
 
 

Answers (4)