Greeting all
I've faced this problem, and I don't know what the cause for it.
See table T_SQL code
- CREATE TABLE [dbo].[Categories] (
- [C_Id] INT IDENTITY (1, 1) NOT NULL,
- [C_Name] NVARCHAR (50) NULL,
- [C_Description] NVARCHAR (500) NULL,
- [C_Enable] BIT NULL,
- [Cat_photo] NVARCHAR (500) NULL,
- [CPhoto_Name] NVARCHAR (50) NULL,
- PRIMARY KEY CLUSTERED ([C_Id] ASC)
- );
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 :
- protected void GV_Category_RowCommand(object sender, GridViewCommandEventArgs e)
- {
- //Get GridView RowIndex
- GridViewRow gvr = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
- int rowid = gvr.RowIndex;
- //End GridView RowIndex
- using (SqlConnection sqlconUpcat = new SqlConnection(connectionString))
- if (e.CommandName == "Update")
- {
- try
- {
- FileUpload FUgvCat= (FileUpload)GV_Category.Rows[rowid].FindControl("FU_CIMG_E");
- if (FUgvCat.HasFile)
- {
- string FN_CatImage = FUgvCat.PostedFile.FileName;
- FUgvCat.SaveAs(Server.MapPath("~/images/categories/" + Path.GetFileName(FN_CatImage)));
- string photopath = "~/images/categories/" + Path.GetFileName(FN_CatImage);
- //Open Connection with D.B
- 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)";
- sqlconUpcat.Open();
- //=============================================
- SqlCommand sqlcmdUCat = new SqlCommand(Query_Up, sqlconUpcat);
- sqlcmdUCat.Parameters.AddWithValue("@C_Name", (GV_Category.Rows[rowid].FindControl("TB_CNameE") as TextBox).Text);
- sqlcmdUCat.Parameters.AddWithValue("@C_Description", (GV_Category.Rows[rowid].FindControl("TB_Cdesc") as TextBox).Text);
- sqlcmdUCat.Parameters.AddWithValue("@C_Enable", (GV_Category.Rows[rowid].FindControl("ChBCE") as CheckBox).Checked);
- //sqlcmdUCat.Parameters.AddWithValue("@Cat_photo",photopath);
- sqlcmdUCat.Parameters.AddWithValue("@CPhoto_Name", (Path.GetFileName(FN_CatImage)));
- sqlcmdUCat.Parameters.AddWithValue("@C_Id", Convert.ToInt32(GV_Category.DataKeys[rowid].Value.ToString()));
- //=============================================
- sqlcmdUCat.ExecuteNonQuery();
- GV_Category.EditIndex = -1;
- }//===>End String Query
- lbSCatMsg.Text = "Success";
- }//End If (File Upload has file)
- }//End Try
- catch (Exception ex)
- {
- lbFCatMsg.Text = ex.Message;
- }
- }//===>End IF (Update)
- }
- protected void GV_Category_RowCommand(object sender, GridViewCommandEventArgs e)
- {
- //Get GridView RowIndex
- GridViewRow gvr = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
- int rowid = gvr.RowIndex;
- //End GridView RowIndex
- using (SqlConnection sqlconUpcat = new SqlConnection(connectionString))
- if (e.CommandName == "Update")
- {
- try
- {
- FileUpload FUgvCat= (FileUpload)GV_Category.Rows[rowid].FindControl("FU_CIMG_E");
- if (FUgvCat.HasFile)
- {
- //upload the file onto the server
- string FN_CatImage = FUgvCat.PostedFile.FileName;
- FUgvCat.SaveAs(Server.MapPath("~/images/categories/" + Path.GetFileName(FN_CatImage)));
- string photopath = "~/images/categories/" + Path.GetFileName(FN_CatImage);
- //Declear variblaes
- TextBox TBName = (GV_Category.Rows[rowid].FindControl("TB_CNameE") as TextBox);
- TextBox TBDEsc = (GV_Category.Rows[rowid].FindControl("TB_Cdesc") as TextBox);
- CheckBox CHEn = (GV_Category.Rows[rowid].FindControl("ChBCE") as CheckBox);
- int Catid = Convert.ToInt32(GV_Category.DataKeys[rowid].Value.ToString());
- //Open Connection with D.B
- sqlconUpcat.Open();
- {
- 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);
- sqlcmdUCat.ExecuteNonQuery();
- GV_Category.EditIndex = -1;
- }//===>End String Query
- lbSCatMsg.Text = "Success"
- }//End If (File Upload has file)
- }//End Try
- catch (Exception ex)
- {
- lbFCatMsg.Text = ex.Message;
- }
- }//===>End IF (Update)
- }
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.

Laxmidhar SahooPosted Aug 8, 2020, 5:35 AM
Aktham MahmoudPosted Aug 7, 2020, 8:32 AM
Aktham MahmoudPosted Aug 7, 2020, 8:31 AM
Jenith ThakkarPosted Aug 6, 2020, 11:20 PM
Thank you