Some Times happens that we tries to Show in Image in Box using Image controls. Developers use different Kinds of Strategies like to store the Photos in Folder and Load Photo from this Folder. But It does not works Sometimes. The better way is to store image in database in Binary Format and show it on web page. Now How can we do it given below Steps:
Step 1: Upload and Insert Image into database Table
protected void BtnUpload_Click(object
sender, EventArgs e)
{
System.Drawing.Image
imag = System.Drawing.Image.FromStream(
FileUpload1.PostedFile.InputStream);
System.Data.SqlClient.SqlConnection conn = null;
try
{
try
{
conn = new
System.Data.SqlClient.SqlConnection(@"server=ADIMPS-DEL-075\SQLEXPRESS;trusted_connection
= yes;database = sample;");
conn.Open();
System.Data.SqlClient.SqlCommand insertCommand = new System.Data.SqlClient.SqlCommand("Insert into [emp](id,name,city,Head_id,Emp_Image)
Values (008,'Nishchal','Gzb','9',@Pic)", conn);
insertCommand.Parameters.Add("@Pic", SqlDbType.Image,
0).Value = ConvertImgToByteArr(imag, System.Drawing.Imaging.ImageFormat.Jpeg);
int
queryResult = insertCommand.ExecuteNonQuery();
if
(queryResult == 1)
LblMsg.Text = "msg record Created Successfully";
}
catch
(Exception ex)
{
LblMsg.Text = "Error: " + ex.Message;
}
}
finally
{
if
(conn != null)
conn.Close();
}
ShowDataList();
}
// function to
Convert Image to Byte Arrary//
private byte[] ConvertImgToByteArr(System.Drawing.Image imageToConvert,System.Drawing.Imaging.ImageFormat formatOfImage)
{
byte[]
ByteValue;
try
{
using
(MemoryStream ms = new
MemoryStream())
{
imageToConvert.Save(ms,
formatOfImage);
ByteValue = ms.ToArray();
}
}
catch (Exception) { throw;
}
return
ByteValue;
}
public void ShowDataList()
{
SqlConnection
conn = new SqlConnection(@"server=DEL-075\SQLEXPRESS;trusted_connection
= yes;database = sample;");
string
sql1 = "select * from emp where emp_image is
not null";
SqlDataAdapter
da = new SqlDataAdapter(sql1,
conn);
DataSet
ds = new DataSet();
da.Fill(ds, "DataL");
DataList1.DataSource = ds.Tables["DataL"];
DataList1.DataBind();
}
Step 2: Make Page Named Picture.aspx and Write this Code on its Load Event.
protected void Page_Load(object
sender, EventArgs e)
{
SqlConnection
conn = new SqlConnection(@"server=ADIMPS-DEL-075\SQLEXPRESS;trusted_connection
= yes;database = sample;");
string
sql1 = "Select Emp_Image from emp where
id='" +Request.QueryString["id"]
+ "'";
SqlCommand
cmd1 = new SqlCommand(sql1,
conn);
conn.Open();
SqlDataReader
dr = cmd1.ExecuteReader();
if
(dr.Read())
{
if
(dr["Emp_Image"].ToString() !=
System.DBNull.Value.ToString())
{
byte[]
imageByte = (byte[])dr["Emp_Image"];
if
(imageByte != null && imageByte.Length
> 0)
{
Context.Response.ContentType = "image/jpeg";
Context.Response.BinaryWrite(imageByte);
}
}
}
dr.Close();
conn.Close();
}
How to Pass from Code Behind:
ProdImg.ImageUrl = "Picture.aspx?id=" + Val(TxtProdId.Text)
How to Pass for bind in Gridview,Datalist,ListView,Datagrid etc. as Given Below:
<asp:Image ID="Image2" runat="server"
Height="80px"
ImageUrl='<%#
"Picture.aspx?id=" + Eval("Product_id")%>' Width="80px" />
No comments:
Post a Comment