In the previous I had
explained ‘How to save image in database in Asp.Net using c#’. In this post I
have explained how to retrieve image from database in asp.net. I suggest you to
follow my previous post ‘How to save image in database in Asp.Net using c#’.
Follow step by step as given below.(First review the
previous post ’how to save image in database in asp.net’).
Step1: Bal
code
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Data;
using
System.Data.SqlClient;
/// <summary>
///
Summary description for bal
/// </summary>
public class bal
{
dal
d1 = new dal();
public
bal()
{
//
// TODO: Add constructor logic here
//
}
public
DataSet find(string
UserName)
{
return d1.find(UserName);
}
}
Step2: Dal code
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Data;
using
System.Data.SqlClient;
/// <summary>
///
Summary description for dal
/// </summary>
public class dal
{
SqlConnection
con = new SqlConnection(@"Data Source=SUSHIL-PC;Initial
Catalog=modi;Integrated Security=True");
public
dal()
{
//
// TODO: Add constructor logic here
//
}
public DataSet find(string
UserName)
{
SqlCommand com = new SqlCommand("sp_search",
con);// here sp_insert is stored procedure
con.Open();
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@username",
UserName);
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
con.Close();
}
}
Util.cs:
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
/// <summary>
///
Summary description for Util
/// </summary>
public class Util
{
public
Util()
{
//
// TODO: Add constructor logic here
//
}
public
static string
IMAGE = "image\\";
public
static string
TILED_ROOT = "~/";
public
static string
ROOT_PATH = System.Web.HttpContext.Current.Server.MapPath("~/");
public
static string makePictureName(
String orignalFileName)
{
return getName(orignalFileName) + "."
+ getExtension(orignalFileName);
}
public
static string
getName(String fileName)
{
if (fileName.Contains("."))
{
return fileName.Split('.')[0].ToString();
}
else
{
//throw Exception;
return fileName;
}
}
public
static string
getExtension(String fileName)
{
if (fileName.Contains("."))
{
return
fileName.Split('.')[1].ToString();
}
else
{
//throw Exception;
return fileName;
}
}
public
static string
makePhysicalPath(String virtualPath)
{
return virtualPath.Replace('\\',
'/');
}
public
static string
makeVirtulPath(String virtualPath)
{
return virtualPath.Replace('/',
'\\');
}
}
Step4: Design page
<!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>
UserName:<asp:TextBox ID="TextBox1"
runat="server"></asp:TextBox><br />
Password:<asp:TextBox ID="TextBox2"
runat="server"></asp:TextBox><br />
Email Id:<asp:TextBox ID="TextBox3"
runat="server"></asp:TextBox><br />
Photo:<asp:FileUpload ID="FileUpload1"
runat="server"
/>
<asp:Image ID="Image1" runat="server" Height="85px" Width="156px" /><br />
<asp:Button ID="Button2" runat="server" Text="Search" onclick="Button2_Click" />
</div>
</form>
</body>
</html>
Step5: .cs page code
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Data;
using
System.Data.SqlClient;
public partial class SaveImageInDatabase : System.Web.UI.Page
{
bal
b1 = new bal();
protected
void Page_Load(object
sender, EventArgs e)
{
}
protected
void Button2_Click(object
sender, EventArgs e)
{
// code for displaying image
string userName = TextBox1.Text;
DataSet _ds = b1.find(userName);
if (_ds != null &&
_ds.Tables[0].Rows.Count > 0)
{
TextBox2.Text=_ds.Tables[0].Rows[0].ItemArray[1].ToString();
TextBox3.Text=_ds.Tables[0].Rows[0].ItemArray[2].ToString();
String s =
_ds.Tables[0].Rows[0].ItemArray[3].ToString();
Image1.ImageUrl = Util.TILED_ROOT + Util.makePhysicalPath(s);
}
}
}
Step6: Run
the code and check the output.
If any
problem regarding this post you can contact me at sushilct86@gmail.com.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.