Mohit Jain

Mohit Jain

  • NA
  • 1
  • 1.7k

File Upload in edit Gridview

Aug 30 2012 6:08 AM
     protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string prod_imgpath;
            Label prod_id = (Label)GridView1.Rows[e.RowIndex].FindControl("lblprodid");
            TextBox prod_title = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txttitle");
            TextBox prod_price = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtprice");
            TextBox prod_Qty = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtqty");
            FileUpload file= (FileUpload)GridView1.Rows[e.RowIndex].FindControl("FileUpload1") as FileUpload;
            if (file.FileName.Length > 0)
            {
                prod_imgpath = file.FileName;
                file.PostedFile.SaveAs(Server.MapPath(@"/Images/" + prod_imgpath));
                SqlDataSource1.UpdateParameters["prod_imgpath"].DefaultValue = file.FileName;
            }
       
            TextBox prod_desc = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtdesc");
            //RadioButtonList prod_isenabled = (RadioButtonList)GridView1.Rows[e.RowIndex].FindControl("Radio1");
            //RadioButtonList prod_isdeleted = (RadioButtonList)GridView1.Rows[e.RowIndex].FindControl("Radio2");
            RadioButtonList rbl1 =(RadioButtonList)GridView1.Rows[e.RowIndex].FindControl("Radio1");
            RadioButtonList rbl2= (RadioButtonList)GridView1.Rows[e.RowIndex].FindControl("Radio2");
            SqlDataSource1.UpdateParameters["prod_isenabled"].DefaultValue = rbl1.SelectedValue;
            SqlDataSource1.UpdateParameters["prod_isdeleted"].DefaultValue = rbl1.SelectedValue;
            string id = prod_id.Text;
            string title = prod_title.Text;
            string price = prod_price.Text;
            string qty = prod_Qty.Text;
            string imgpath = file.FileName;
            string desc = prod_desc.Text;
            string enabled = rbl1.SelectedValue.ToString();
            string deleted = rbl2.SelectedValue.ToString();
            UpdateProduct(id, title, price, qty,imgpath, desc, enabled, deleted);
            GridView1.EditIndex = -1;
            BindGrid();
            
        }


        private void UpdateProduct(string prod_id, string prod_title, string prod_price, string prod_Qty, string prod_imgpath, string prod_desc, string prod_enabled, string prod_deleted)
        {
            cmd = new SqlCommand();
            con.Open();
            cmd.Connection = con;
            cmd.CommandText = "spProduct";
            cmd.CommandType= CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@prod_id", SqlDbType.Int));
            cmd.Parameters.Add(new SqlParameter("@status", SqlDbType.VarChar, 50));
            cmd.Parameters.Add(new SqlParameter("@prod_title", SqlDbType.VarChar, 50));
            cmd.Parameters.Add(new SqlParameter("@prod_price", SqlDbType.VarChar, 50));
            cmd.Parameters.Add(new SqlParameter("@prod_Qty", SqlDbType.VarChar, 50));
            cmd.Parameters.Add(new SqlParameter("@prod_imgpath", SqlDbType.VarChar, 50));
            cmd.Parameters.Add(new SqlParameter("@prod_desc", SqlDbType.VarChar, 50));
            cmd.Parameters.Add(new SqlParameter("@prod_isenabled", SqlDbType.VarChar, 50));
            cmd.Parameters.Add(new SqlParameter("@prod_isdeleted", SqlDbType.VarChar, 50));
            cmd.Parameters["@prod_id"].Value = Convert.ToInt32(prod_id.ToString());
            cmd.Parameters["@status"].Value = "Update";
            cmd.Parameters["@prod_title"].Value = prod_title;
            cmd.Parameters["@prod_price"].Value = prod_price;
            cmd.Parameters["@prod_Qty"].Value = prod_Qty;
            cmd.Parameters["@prod_imgpath"].Value = prod_imgpath;
            cmd.Parameters["@prod_desc"].Value = prod_desc;
            cmd.Parameters["@prod_isenabled"].Value = prod_enabled;
            cmd.Parameters["@prod_isdeleted"].Value = prod_deleted;
            
            da = new SqlDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds);
            con.Close();
        }

    i have gridview in which i have updated the all the field but when i click edit option,if i edit prod title,it's compuslory to edit prod imgpath.