Hi nagendra89,
It is not possible to set value to FileUpload control. It does not allow you to set values, nor it will retain values after postback. For this only solution is place HiddenField control inside FileUpload control to set and get uploaded value.
Check this example. Now please take its reference and correct your code.
HTML
<asp:GridView ID="Gridview1" runat="server" ShowFooter="true" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" placeholder="Enter Name"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" placeholder="Enter Country"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="File">
<ItemTemplate>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Upload">
<ItemTemplate>
<asp:FileUpload ID="fuUpload" runat="server" />
<asp:HiddenField ID="hfFileByte" runat="server" />
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" OnClick="ButtonAdd_Click" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button Text="Insert" runat="server" OnClick="Save" />
Namespaces
C#
using System;
using System.Data;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
VB.Net
Imports System
Imports System.Data
Imports System.IO
Imports System.Web.UI
Imports System.Web.UI.WebControls
Code
C#
private void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));
dt.Columns.Add(new DataColumn("Column3", typeof(string)));
dt.Columns.Add(new DataColumn("Column4", typeof(byte[])));
dr = dt.NewRow();
dr["RowNumber"] = 1;
dr["Column1"] = string.Empty;
dr["Column2"] = string.Empty;
dr["Column3"] = string.Empty;
dr["Column4"] = null;
dt.Rows.Add(dr);
//Store the DataTable in ViewState
ViewState["CurrentTable"] = dt;
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
private void AddNewRowToGrid()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
HiddenField hfFile = (HiddenField)Gridview1.Rows[rowIndex].Cells[4].FindControl("hfFileByte");
FileUpload fuUpload = (FileUpload)Gridview1.Rows[rowIndex].Cells[4].FindControl("fuUpload");
byte[] bytes = null;
if (fuUpload.HasFile)
{
BinaryReader br = new BinaryReader(fuUpload.PostedFile.InputStream);
bytes = br.ReadBytes((int)fuUpload.PostedFile.InputStream.Length);
hfFile.Value = Convert.ToBase64String(bytes);
}
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i + 1;
dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;
dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;
dtCurrentTable.Rows[i - 1]["Column3"] = fuUpload.HasFile ? Path.GetFileName(fuUpload.PostedFile.FileName) : box3.Text;
dtCurrentTable.Rows[i - 1]["Column4"] = fuUpload.HasFile ? bytes : Convert.FromBase64String(hfFile.Value);
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
Gridview1.DataSource = dtCurrentTable;
Gridview1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
SetPreviousData();
}
private void SetPreviousData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
HiddenField hfFile = (HiddenField)Gridview1.Rows[rowIndex].Cells[4].FindControl("hfFileByte");
box1.Text = dt.Rows[i]["Column1"].ToString();
box2.Text = dt.Rows[i]["Column2"].ToString();
box3.Text = dt.Rows[i]["Column3"].ToString();
hfFile.Value = !Convert.IsDBNull(dt.Rows[i]["Column4"]) ? Convert.ToBase64String((byte[])dt.Rows[i]["Column4"]) : "";
rowIndex++;
}
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SetInitialRow();
}
}
protected void ButtonAdd_Click(object sender, EventArgs e)
{
AddNewRowToGrid();
}
protected void Save(object sender, EventArgs e)
{
foreach (GridViewRow row in Gridview1.Rows)
{
string name = (row.FindControl("TextBox1") as TextBox).Text;
string country = (row.FindControl("TextBox2") as TextBox).Text;
string fileName = (row.FindControl("TextBox3") as TextBox).Text;
// Retriving Uploaded File value from Hiddenfield.
string base64String = (row.FindControl("hfFileByte") as HiddenField).Value;
byte[] bytes = Convert.FromBase64String(base64String);
string filePath = Server.MapPath("~/Files/" + fileName);
if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(country) && !string.IsNullOrEmpty(fileName) && bytes != null)
{
// Save the Byte Array as file in folder.
File.WriteAllBytes(filePath, bytes);
// Insert Code goes here.
// Insert record into database either with path or with binart data.
}
}
}
VB.Net
Private Sub SetInitialRow()
Dim dt As DataTable = New DataTable()
Dim dr As DataRow = Nothing
dt.Columns.Add(New DataColumn("RowNumber", GetType(String)))
dt.Columns.Add(New DataColumn("Column1", GetType(String)))
dt.Columns.Add(New DataColumn("Column2", GetType(String)))
dt.Columns.Add(New DataColumn("Column3", GetType(String)))
dt.Columns.Add(New DataColumn("Column4", GetType(Byte())))
dr = dt.NewRow()
dr("RowNumber") = 1
dr("Column1") = String.Empty
dr("Column2") = String.Empty
dr("Column3") = String.Empty
dr("Column4") = Nothing
dt.Rows.Add(dr)
ViewState("CurrentTable") = dt
Gridview1.DataSource = dt
Gridview1.DataBind()
End Sub
Private Sub AddNewRowToGrid()
Dim rowIndex As Integer = 0
If ViewState("CurrentTable") IsNot Nothing Then
Dim dtCurrentTable As DataTable = CType(ViewState("CurrentTable"), DataTable)
Dim drCurrentRow As DataRow = Nothing
If dtCurrentTable.Rows.Count > 0 Then
For i As Integer = 1 To dtCurrentTable.Rows.Count
Dim box1 As TextBox = CType(Gridview1.Rows(rowIndex).Cells(1).FindControl("TextBox1"), TextBox)
Dim box2 As TextBox = CType(Gridview1.Rows(rowIndex).Cells(2).FindControl("TextBox2"), TextBox)
Dim box3 As TextBox = CType(Gridview1.Rows(rowIndex).Cells(3).FindControl("TextBox3"), TextBox)
Dim hfFile As HiddenField = CType(Gridview1.Rows(rowIndex).Cells(4).FindControl("hfFileByte"), HiddenField)
Dim fuUpload As FileUpload = CType(Gridview1.Rows(rowIndex).Cells(4).FindControl("fuUpload"), FileUpload)
Dim bytes As Byte() = Nothing
If fuUpload.HasFile Then
Dim br As BinaryReader = New BinaryReader(fuUpload.PostedFile.InputStream)
bytes = br.ReadBytes(CInt(fuUpload.PostedFile.InputStream.Length))
hfFile.Value = Convert.ToBase64String(bytes)
End If
drCurrentRow = dtCurrentTable.NewRow()
drCurrentRow("RowNumber") = i + 1
dtCurrentTable.Rows(i - 1)("Column1") = box1.Text
dtCurrentTable.Rows(i - 1)("Column2") = box2.Text
dtCurrentTable.Rows(i - 1)("Column3") = If(fuUpload.HasFile, Path.GetFileName(fuUpload.PostedFile.FileName), box3.Text)
dtCurrentTable.Rows(i - 1)("Column4") = If(fuUpload.HasFile, bytes, Convert.FromBase64String(hfFile.Value))
rowIndex += 1
Next
dtCurrentTable.Rows.Add(drCurrentRow)
ViewState("CurrentTable") = dtCurrentTable
Gridview1.DataSource = dtCurrentTable
Gridview1.DataBind()
End If
Else
Response.Write("ViewState is null")
End If
SetPreviousData()
End Sub
Private Sub SetPreviousData()
Dim rowIndex As Integer = 0
If ViewState("CurrentTable") IsNot Nothing Then
Dim dt As DataTable = CType(ViewState("CurrentTable"), DataTable)
If dt.Rows.Count > 0 Then
For i As Integer = 0 To dt.Rows.Count - 1
Dim box1 As TextBox = CType(Gridview1.Rows(rowIndex).Cells(1).FindControl("TextBox1"), TextBox)
Dim box2 As TextBox = CType(Gridview1.Rows(rowIndex).Cells(2).FindControl("TextBox2"), TextBox)
Dim box3 As TextBox = CType(Gridview1.Rows(rowIndex).Cells(3).FindControl("TextBox3"), TextBox)
Dim hfFile As HiddenField = CType(Gridview1.Rows(rowIndex).Cells(4).FindControl("hfFileByte"), HiddenField)
box1.Text = dt.Rows(i)("Column1").ToString()
box2.Text = dt.Rows(i)("Column2").ToString()
box3.Text = dt.Rows(i)("Column3").ToString()
hfFile.Value = If(Not Convert.IsDBNull(dt.Rows(i)("Column4")), Convert.ToBase64String(CType(dt.Rows(i)("Column4"), Byte())), "")
rowIndex += 1
Next
End If
End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Page.IsPostBack Then
SetInitialRow()
End If
End Sub
Protected Sub ButtonAdd_Click(ByVal sender As Object, ByVal e As EventArgs)
AddNewRowToGrid()
End Sub
Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs)
For Each row As GridViewRow In Gridview1.Rows
Dim name As String = (TryCast(row.FindControl("TextBox1"), TextBox)).Text
Dim country As String = (TryCast(row.FindControl("TextBox2"), TextBox)).Text
Dim fileName As String = (TryCast(row.FindControl("TextBox3"), TextBox)).Text
Dim base64String As String = (TryCast(row.FindControl("hfFileByte"), HiddenField)).Value
Dim bytes As Byte() = Convert.FromBase64String(base64String)
Dim filePath As String = Server.MapPath("~/Files/" & fileName)
If Not String.IsNullOrEmpty(name) AndAlso Not String.IsNullOrEmpty(country) AndAlso Not String.IsNullOrEmpty(fileName) AndAlso bytes IsNot Nothing Then
File.WriteAllBytes(filePath, bytes)
End If
Next
End Sub
Screenshot
For saving in database refer below articles.
For Path
For Binary