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)
- {
-
- GridViewRow gvr = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
- int rowid = gvr.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);
-
- 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("@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;
- }
- lbSCatMsg.Text = "Success";
- }
- }
- catch (Exception ex)
- {
- lbFCatMsg.Text = ex.Message;
- }
- }
-
- }
I reusing another method, I said maybe
qlcmdUCat.Parameters.AddWithValue( Can't able to catch object decleared.
- protected void GV_Category_RowCommand(object sender, GridViewCommandEventArgs e)
- {
-
- GridViewRow gvr = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
- int rowid = gvr.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);
-
- 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());
-
-
- sqlconUpcat.Open();
- {
-
-
- sqlcmdUCat.ExecuteNonQuery();
- GV_Category.EditIndex = -1;
- }
- lbSCatMsg.Text = "Success"
-
- }
- }
- catch (Exception ex)
- {
- lbFCatMsg.Text = ex.Message;
- }
- }
- }
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.