Here i am storing the Enroll No and ImagePath in DataBase and redirecting to second page with QueryString Parameters as EnrollNo and FileName. Using the QueryString parameter i am retrieving the last stored record.
First.aspx:
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtEnrollNo" runat="server" />
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
</div>
</form>
First.aspx.cs:
protected void btnUpload_Click(object sender, EventArgs e)
{
if (FileUpload1.PostedFile != null)
{
string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
//Save files to images folder
FileUpload1.SaveAs(Server.MapPath("images/" + FileName));
//Add Entry to DataBase
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
string strQuery = "insert into FileTable (FileName, FilePath,EnrollNo) values(@FileName, @FilePath,@EnrollNo)";
SqlCommand cmd = new SqlCommand(strQuery);
cmd.Parameters.AddWithValue("@FileName", FileName);
cmd.Parameters.AddWithValue("@FilePath", "images/" + FileName);
cmd.Parameters.AddWithValue("@EnrollNo", this.txtEnrollNo.Text.Trim());
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
con.Close();
con.Dispose();
}
Response.Redirect("SecondPage.aspx?EnrollNo=" + this.txtEnrollNo.Text.Trim() + "&FileName=" + FileName + "");
}
}
Second.aspx:
<form id="form1" runat="server">
<div>
Image name
<asp:Label ID="lblFileName" runat="server" />
<br />
Image
<asp:Image ID="imgImage" runat="server" />
</div>
</form>
C#:
private int EnrollNo
{
get
{
return int.Parse(Server.UrlDecode(Request.QueryString["EnrollNo"]));
}
}
private string FileName
{
get
{
return Server.UrlDecode(Request.QueryString["FileName"]);
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.GetData();
}
}
private void GetData()
{
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
string strQuery = "SELECT * FROM FileTable Where FileName = @FileName AND EnrollNo = @EnrollNo";
SqlCommand cmd = new SqlCommand(strQuery, con);
cmd.Parameters.AddWithValue("@FileName", this.FileName);
cmd.Parameters.AddWithValue("@EnrollNo", this.EnrollNo);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
this.lblFileName.Text = ds.Tables[0].Rows[0]["FileName"].ToString();
this.imgImage.ImageUrl = ds.Tables[0].Rows[0]["FilePath"].ToString();
}
}
SQL:
CREATE TABLE [dbo].[FileTable](
[ID] [int] IDENTITY(1,1) NOT NULL,
[FileName] [varchar](50) NOT NULL,
[FilePath] [varchar](200) NOT NULL,
[EnrollNo] [int] NULL
) ON [PRIMARY]
GO

You have to create image folder in your project to store the images. I have Created the Folder under my project Directory
Here FileUpload.apsx is my Fisrt.aspx page that i mentioned in code.
Thank You.