Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I want to update image using Edit,Update using C# asp.net gridview,
So, How to call FileUpload in Gridview cell in C# asp.net?

I placed FileUpload in Gridview Cell but when we call image using file upload, it,s blank image going in to the variable, I am using this code for Update Image:-

C#
FileUpload Image1 = (FileUpload)GVStartCampaignDetails.Rows[e.RowIndex].FindControl("Image1");// Image it's filling blank in Image Field.
        string filename = System.IO.Path.GetFileName(Image1.FileName);
        Session["UpdateImage"] = filename;
Image1.SaveAs(Server.MapPath("Templates/") + filename + ".jpg");
                    SqlConnection con = new SqlConnection(connectionString);
                    con.Open();
                    SqlCommand cmd = new SqlCommand("procUpdateImage", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@EmailId", SqlDbType.NVarChar).Value = EmailId;
cmd.Parameters.Add("@ImagePath", SqlDbType.NVarChar).Value = Session["UpdateImage"].ToString();
cmd.ExecuteNonQuery();
con.close();



Please Help me.

Thanks in Advance.
Posted
Updated 14-Jan-14 19:11pm
v3

 
Share this answer
 
Comments
[no name] 15-Jan-14 1:55am    
Hi, Karthik

I am using this code for image update in gridview but blank image it's getting in this (Image1) variable.
Please help me.

GridViewRow row = GVDetails.Rows[e.RowIndex];
//FileUpload Image1 = (FileUpload)GVDetails.Rows[e.RowIndex].FindControl("Image1");
FileUpload Image1 = row.Cells[5].FindControl("Image1") as FileUpload;
if (Image1 != null && Image1.HasFile)
{
Image1.SaveAs(Server.MapPath("Templates/") + Image1.FileName + ".jpg");
}
Karthik_Mahalingam 15-Jan-14 1:58am    
Image1 is the name of fileupload control ??
Siva Hyderabad 14-Mar-14 7:54am    
+5
[no name] 15-Jan-14 3:47am    
Yes
Karthik_Mahalingam 15-Jan-14 3:50am    
can u post your entire grid code,
aspx as well code behind..
i have to trace it.
In aspx
ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FileUploadInsideGridView.aspx.cs" Inherits="FileUploadInsideGridView" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataKeyNames="PersonID" DataSourceID="SqlDataSource1"
            onrowcommand="GridView1_RowCommand" >
            <Columns>
                <asp:BoundField DataField="FirstName" HeaderText="FirstName"
                    SortExpression="FirstName" />
                <asp:BoundField DataField="LastName" HeaderText="LastName"
                    SortExpression="LastName" />
                <asp:TemplateField HeaderText="Upload">
                    <ItemTemplate>
                        <asp:FileUpload ID="FileUpload1" runat="server" EnableViewState="true" />
                        <asp:Button ID="saveBtn" runat="server"
                            CommandArgument="<%# Container.DataItemIndex%>" CommandName="save"
                            Text="OK"/>
                    </ItemTemplate>
                </asp:TemplateField>
                <%--<asp:ButtonField ButtonType="Button" CommandName="save" Text="Save"/>--%>
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:SomeConnectionString %>"
            SelectCommand="SELECT [PersonID], [FirstName], [LastName] FROM [Person]">
        </asp:SqlDataSource>
    </div>
    </form>
</body>
</html>


In Code behind

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class FileUploadInsideGridView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("save")) 
        {
            //GridView Row Index
            int rowIndex = int.Parse( e.CommandArgument.ToString().Trim());

            // ID of the Current Row
            String ID = GridView1.DataKeys[rowIndex].Values["PersonID"].ToString().Trim();

            //File Upload Instance of the Current Row
            FileUpload fileUpload = (FileUpload)GridView1.Rows[rowIndex].FindControl("FileUpload1");

            //File
            System.IO.Stream stream = fileUpload.PostedFile.InputStream;
            
            //File Length
            int length = fileUpload.PostedFile.ContentLength;
            
            //Content of the File
            byte[] data = new byte[length];
            
            //Fill data
            stream.Read(data, 0, length);

            //File Extension
            String extension = System.IO.Path.GetExtension(fileUpload.PostedFile.FileName);
        }
    }
}


Refer:
http://forums.asp.net/t/1627633.aspx[^]
how to manage file upload control inside grid view how to get the path of file in file upload[^]

This may help.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900