Hi Vanessa,
Please refer below sample.
Database
I have used a table named Files having 3 columns Id, Name and FilePath.
HTML
Default
<asp:FileUpload ID="fileUpload1" runat="server" /><br />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="Upload" />
<asp:GridView ID="gvFiles" CssClass="Gridview" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Name" HeaderText="FileName" />
<asp:BoundField DataField="Path" HeaderText="FilePath" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnView" Text="View" runat="server" OnClick="OnSend" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Home
File Name:<asp:Label ID="lblName" runat="server" />
<br />
File Path:<asp:Label ID="lblPath" runat="server" />
Namespaces
Default
C#
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Code
Default
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
protected void Upload(object sender, EventArgs e)
{
string fileName = Path.GetFileName(fileUpload1.PostedFile.FileName);
fileUpload1.SaveAs(Server.MapPath("~/Files/" + fileName));
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Files(Name,Path) VALUES(@Name,@Path)", con))
{
cmd.Parameters.AddWithValue("@Name", fileName);
cmd.Parameters.AddWithValue("@Path", "Files/" + fileName);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
BindGrid();
}
protected void OnSend(object sender, EventArgs e)
{
GridViewRow row = (sender as Button).NamingContainer as GridViewRow;
string filename = row.Cells[0].Text;
string filepath = row.Cells[1].Text;
Session["Name"] = filename;
Session["Path"] = filepath;
Response.Redirect("Home.aspx");
}
private void BindGrid()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT Name,Path FROM Files", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
gvFiles.DataSource = ds;
gvFiles.DataBind();
}
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindGrid()
End If
End Sub
Protected Sub Upload(ByVal sender As Object, ByVal e As EventArgs)
Dim fileName As String = Path.GetFileName(fileUpload1.PostedFile.FileName)
fileUpload1.SaveAs(Server.MapPath("~/Files/" & fileName))
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("INSERT INTO Files(Name,Path) VALUES(@Name,@Path)", con)
cmd.Parameters.AddWithValue("@Name", fileName)
cmd.Parameters.AddWithValue("@Path", "Files/" & fileName)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
BindGrid()
End Sub
Protected Sub OnSend(ByVal sender As Object, ByVal e As EventArgs)
Dim row As GridViewRow = TryCast((TryCast(sender, Button)).NamingContainer, GridViewRow)
Dim filename As String = row.Cells(0).Text
Dim filepath As String = row.Cells(1).Text
Session("Name") = filename
Session("Path") = filepath
Response.Redirect("HomeVB.aspx")
End Sub
Private Sub BindGrid()
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("SELECT Name,Path FROM Files", con)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Using ds As DataSet = New DataSet()
sda.Fill(ds)
gvFiles.DataSource = ds
gvFiles.DataBind()
End Using
End Using
End Using
End Using
End Sub
Home
C#
protected void Page_Load(object sender, EventArgs e)
{
lblName.Text = Session["Name"].ToString();
lblPath.Text = Session["Path"].ToString();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
lblName.Text = Session("Name").ToString()
lblPath.Text = Session("Path").ToString()
End Sub
Screenshot