locked
how to use file upload control in footer of gridview ad show files in grid view after uploading RRS feed

  • Question

  • User-1936440448 posted

    hello ,

    i'm using file upload in footer of gridview and i want to store data in datatable sothat i can see files in gridview .

    my code is............

    public void binding()
        {
            if (ViewState["ss"] == null)
            {
                DataTable dt = new DataTable("");
                DataRow dr = dt.NewRow();
                dt.Rows.Add(dr);
                ViewState["ss"] = dt;
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }
       
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "save")
            {

                DataTable ab = (DataTable)(Session["ss"]);
                
                
                    FileUpload fi = ((FileUpload)(GridView1.FooterRow.FindControl("FileUpload1")));
                    String s = fi.FileName;
                    if (s != "")
                    {
                            fi.PostedFile.SaveAs(Server.MapPath("filess" + "//"+ s));
                    }
                    ViewState["img"] = s;
                    GridView1.DataSource = ab;
                    GridView1.DataBind();
            
                    
                
            }

    Thursday, March 7, 2013 3:05 AM

All replies

  • User1610357552 posted

    Code for upload image

    --------------------------------------------------------------------------------------------------------------------

      protected void Button1_Click1(object sender, EventArgs e)
        {

            Button b1 = (Button)sender;
            GridViewRow row = (GridViewRow)b1.Parent.Parent;
            int idx = row.RowIndex;

            ObjCon.Open();

     FileUpload ImageUpload = (FileUpload)row.Cells[0].FindControl("FileUpload1");

            Button ButtonClick = (Buttonrow.Cells[0].FindControl("Button1");
            ViewState["ButtonClick"] = ButtonClick.UniqueID;

            if (ImageUpload.HasFile)
            {

                string filename = Path.GetFileName(ImageUpload.FileName);
                string strFullPath = "~/UploadImages/" + filename;
                ViewState["ImageFullPath"] = strFullPath;
                ImageUpload.SaveAs(Server.MapPath("~/UploadImages/") + filename);
                ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Upload status: File uploaded!')</script>");

            }

    }

    ------------------------------------------------------------------------------------------------------------------

    Code for update database

      protected void girdview1_RowUpdating(object sender, GridViewRowEventArgs e)
        {
            string strButtonClick = Convert.ToString(ViewState["ButtonClick"]);

                if (!string.IsNullOrEmpty(strButtonClick))
                {

      String ProductImage = ViewState["ImageFullPath"].ToString();

       string ObjStr = "UPDATE  AdminProductEdit SET AProdImage='" + ProductImage +"WHERE AProdId=" + ProductID;

      SqlCommand ObjCmd = new SqlCommand(ObjStr, ObjCon);

                    ObjCon.Open();
                    ObjCmd.ExecuteNonQuery();
                    ObjCon.Close();

    }

    }

    Thursday, March 7, 2013 4:21 AM
  • User-1936440448 posted

    thnax for reply madam ............. i 'm trying regarding u

    Thursday, March 7, 2013 5:50 AM
  • User-1936440448 posted

    its donst work madam.............

    my ques is different pls read

    Thursday, March 7, 2013 8:13 AM
  • User-1405118418 posted

    please correct this fileupload  near if condition and mappath in insert statement these are terminated while compilation

    protected void updateRecord(object sender, GridViewUpdateEventArgs e)
    {

    FileUpload fuimage = GridView1.Rows[e.RowIndex].FindControl("fuimage") as FileUpload;

    Guid FileName = Guid.NewGuid();
    if (fuimage.FileName != "")
    {
    fuimage.SaveAs(Server.MapPath("" + FileName + ".png"));
    GlobalClass.dt.Rows[GridView1.Rows[e.RowIndex].RowIndex]["photopath"] = "~/Images/" + FileName + ".png";

    File.Delete(Server.MapPath(GlobalClass.imageEditPath));
    }

    GlobalClass.adap.Update(GlobalClass.dt);
    GridView1.EditIndex = -1;
    FillGridView();

    }

    protected void InsertNewRecord(object sender, EventArgs e)
    {

    FileUpload fuimage = GridView1.FooterRow.FindControl("fuNewimage") as FileUpload;

    Guid FileName = Guid.NewGuid();
    fuimage.SaveAs(Server.MapPath("~/Images/") + FileName);
    DataRow dr = GlobalClass.dt.NewRow();

    dr["photopath"] = "~/Images/" + FileName + ".png";
    GlobalClass.dt.Rows.Add(dr);
    GlobalClass.adap.Update(GlobalClass.dt);
    GridView1.ShowFooter = false;
    FillGridView();

    }

    Friday, June 17, 2016 7:06 AM