Hi Tevin,
Using the below Article i have created the example.
Please refer below sample.
HTML
<asp:Repeater ID="rpttblFiles" runat="server">
<ItemTemplate>
<asp:Label ID="lblId" runat="server" Text='<%# Eval("Id") %>'></asp:Label>
<br />
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
<br />
<asp:Button ID="btnSave" Text="Save" runat="server" OnClick="Save" />
Namespace
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.IO
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("Select * From tblFiles", con))
{
cmd.CommandType = CommandType.Text;
con.Open();
this.rpttblFiles.DataSource = cmd.ExecuteReader();
this.rpttblFiles.DataBind();
con.Close();
}
}
}
}
protected void Save(object sender, EventArgs e)
{
foreach (RepeaterItem item in rpttblFiles.Items)
{
int id = int.Parse((item.FindControl("lblId") as Label).Text);
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "Select Name,Data from tblFiles where id=@id";
cmd.Parameters.AddWithValue("@id", id);
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
sdr.Read();
byte[] bytes = (byte[])sdr["Data"];
string fileName = sdr["Name"].ToString();
string filePath = "~/Files/" + Path.GetFileName(fileName);
File.WriteAllBytes(Server.MapPath(filePath), bytes);
}
}
con.Close();
}
}
}
VB.Net
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("Select * From tblFiles", con)
cmd.CommandType = CommandType.Text
con.Open()
Me.rpttblFiles.DataSource = cmd.ExecuteReader()
Me.rpttblFiles.DataBind()
con.Close()
End Using
End Using
End If
End Sub
Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs)
For Each item As RepeaterItem In rpttblFiles.Items
Dim id As Integer = Integer.Parse((TryCast(item.FindControl("lblId"), Label)).Text)
Dim constring As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constring)
Using cmd As SqlCommand = New SqlCommand()
cmd.CommandText = "Select Name,Data from tblFiles where id=@id"
cmd.Parameters.AddWithValue("@id", id)
cmd.Connection = con
con.Open()
Using sdr As SqlDataReader = cmd.ExecuteReader()
sdr.Read()
Dim bytes As Byte() = CType(sdr("Data"), Byte())
Dim fileName As String = sdr("Name").ToString()
Dim filePath As String = "~/Files/" & Path.GetFileName(fileName)
File.WriteAllBytes(Server.MapPath(filePath), bytes)
End Using
End Using
con.Close()
End Using
Next
End Sub
Output
Files will be downloaded in the Files folder inside the project folder.