Hi friends, today I will say you “how
to store an image into database and how to retrieve image from database??”
Normally to upload an image into
your application we use the tag called “Upload”.
Store.aspx:
<asp:FileUpload
ID="FileUpload1"
runat="server"
/>
Store.aspx.cs:
int img = FileUpload1.PostedFile.ContentLength;
byte[]
msdata = new byte[img];
FileUpload1.PostedFile.InputStream.Read(msdata,
0, img);
SqlParameter
p1 = new SqlParameter("@bill",
msdata);
cmd.Parameters.Add(p1);
There is a data type known as “image”
which is used to store an image into database tables. Normally, in user point
of view the image is stored but if we see the data in the table it will store
it in the form of binary code. This is because normally an image is consists of
pixels that we can’t store in to our tables. So, database itself will store it
in the form of binary code.
Here comes the real problem.....
If the user wants to retrieve image from the database “He can’t .... Why??”
because it was stored as binary code. To convert binary code into image we will
use “Handlers”. First we must create a “Handler” and we will use that handler
in our application where we need to display an image.
Handler.ashx:
<%@
WebHandler Language="C#" CodeBehind="Handler1.ashx.cs" Class="EEM.Handler1" %>
Handler1.ashx.cs:
using
System.Web;
using
System.Data;
using
System.Data.SqlClient;
using
System.Drawing;
namespace
EEM
{
public class Handler1 :
IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=Santosh-PC\\SQLEXPRESSGH;Initial
Catalog=EEM;Integrated Security=True";
// Create
SQL Command
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Select Ticketcopies from Travel" +
" where Voucherid=@Voucherid";
cmd.CommandType =
System.Data.CommandType.Text;
cmd.Connection = con;
SqlParameter ImageID = new SqlParameter("@Voucherid",
System.Data.SqlDbType.Int);
ImageID.Value =
context.Request.QueryString["Voucherid"];
cmd.Parameters.Add(ImageID);
con.Open();
SqlDataReader dReader =
cmd.ExecuteReader();
dReader.Read();
context.Response.BinaryWrite((byte[])dReader["bill"]);
dReader.Close();
con.Close();
}
public bool IsReusable
{
get
{
return
false;
}
}
}
}
How to use handler in our
application
<asp:GridView ID="GridView1"
runat="server"
AllowPaging="True"
AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSource1"
ForeColor="#333333" GridLines="None">
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:BoundField
DataField="VoucherId"
HeaderText="VoucherId"
SortExpression="VoucherId" />
<asp:TemplateField
HeaderText="Bill">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" Height="150px" ImageUrl='<%#"Handler.ashx?VoucherId=" +
Eval("voucherid")%>' Width="150px" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#0CB9BB" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#0CB9BB" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White"
/>
</asp:GridView>
<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:EEMConnectionString %>"
SelectCommand="SELECT *
FROM [Medical]"></asp:SqlDataSource>