Monday, August 29, 2011

Store and Retrive Images using SQL SERVER 2008

Retriving Images From DataBase


To Make Zoom--> Press ctrl & scrole Mouse.....OR Press Ctrl & perss ' + '  Key

note:
1) In VS Both Handler.ashx and Default.aspx should be in Same Folder or Same Tree level
2) Create table With name tbl_ModuleImages and columns
as
ImageId            Int,
ImageName    Varchar(50),
Image              image



==============================
Add New Item class.cs file and name it as connection and write code as
-----------------------------

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

/// <summary>
/// Summary description for Connection
/// </summary>
public class Connection
{
public SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constring"].ToString());
public SqlCommand cmd = new SqlCommand();
public SqlDataReader dr;
public SqlDataAdapter da=new SqlDataAdapter();
public DataSet ds=new DataSet();

}
-------------------------------------------

add new item->generic Handler file name it as Handler.ashx file & Write as
----------------------

using System.Web;

using System.Data;
using System.Data.SqlClient;


public class Handler : IHttpHandler
{
Connection obj = new Connection();
public void ProcessRequest (HttpContext context)
{
obj.con.Close();
obj.con.Open();
obj.cmd = new SqlCommand("Select ImageName,Image from tbl_ModuleImages where ImageID "+
"=@ImageID",obj.con);
obj.cmd.CommandType = CommandType.Text;
SqlParameter ImageID = new SqlParameter("@ImageID", System.Data.SqlDbType.Int);
ImageID.Value = context.Request.QueryString["ImageID"];
obj.cmd.Parameters.Add(ImageID);
obj.dr=obj.cmd.ExecuteReader();
obj.dr.Read();
context.Response.BinaryWrite((Byte[])obj.dr["Image"]);
obj.dr.Close();
obj.con.Close();
}
}

==============================================================================================
go aspx page take one DataList Control and drag and drop it on default.aspx page and
default.aspx.cs page write as
-------------------------
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;


public partial class _Default : System.Web.UI.Page
{
Connection obj = new Connection();
protected void Page_Load(object sender, EventArgs e)
{
obj.con.Close();
obj.con.Open();
obj.cmd = new SqlCommand("Select * from tbl_ModuleImages",obj.con);
obj.da = new SqlDataAdapter(obj.cmd);
obj.da.Fill(obj.ds,"asd");
GridView2.DataSource = obj.ds;
GridView2.DataBind();
DataList1.DataSource = obj.ds;
DataList1.DataBind();
obj.con.Close();
}
}
---------------------------------------------------------------------



Add new Item ->Default.aspx and
next go to source code of default.aspx html code and write as
 ---------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>
</div>
//For gridView Control using Datasource Wizard
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ImageID" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="ImageID" HeaderText="ImageID" InsertVisible="False"
ReadOnly="True" SortExpression="ImageID" />
<asp:BoundField DataField="ImageName" HeaderText="ImageName"
SortExpression="ImageName" />
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" Height="10px" Width="10px" ImageUrl='<%# "Handler.ashx?ImageID=" + Eval("ImageID")%>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:dashboardConnectionString %>"
SelectCommand="SELECT * FROM [tbl_ModuleImages]"></asp:SqlDataSource>




// For GridView Control to Display Images


<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="ImageID" HeaderText="ImageID" />
<asp:BoundField DataField="ImageName" HeaderText="ImageName" />
<asp:TemplateField>
<ItemTemplate>
<asp:Image ID="Img1" runat="server" Height="10px" Width="10px" ImageUrl='<%#"Handler.ashx?ImageID=" +Eval("ImageID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


// for datalist control

<asp:DataList ID="DataList1" RepeatColumns="3" RepeatDirection="Horizontal" runat="server">
<ItemTemplate>
<asp:Image ID="img2" runat="server" Height="150px" Width="200px" ImageUrl='<%#"Handler.ashx?ImageId="+ Eval("ImageID") %>' /><br />
<asp:Label ID="lb4" runat="server" Text='<%# Eval("ImageName") %>'></asp:Label><br />
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ImageId") %>'></asp:Label><br />

</ItemTemplate>
</asp:DataList>
</form>
</body>
</html>
===============================================================
OutPut:
------



No comments: