Design the Form as Follows:
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="EmpId"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" MaxLength="3"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="Ename"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server" Style="margin-top: 0px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text="Emp Image"></asp:Label>
</td>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:Button ID="Button1" runat="server" Text="Save" OnClick="Button1_Click" />
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label4" runat="server"></asp:Label>
</td>
</tr>
</table>
</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="FALSE">
<Columns>
<asp:BoundField HeaderText="EmpId" DataField="EmpId" />
<asp:BoundField HeaderText="Ename" DataField="Ename" />
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "Handler.ashx?ImID="+ Eval("EmpId") %>' Height="150px" Width="150px" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
.cs Page
string sqlc = System.Configuration.ConfigurationManager.ConnectionStrings["SqlConnection"].ToString();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(sqlc);
SqlCommand cmd=new SqlCommand();
Stream fs = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
con.Open();
cmd.Parameters.AddWithValue("@EmpId", Convert.ToInt32(TextBox1.Text));
cmd.Parameters.AddWithValue("@Ename", TextBox2.Text.ToString());
cmd.Parameters.Add("@Eimage", SqlDbType.Binary).Value = bytes;
cmd.Connection=con;
cmd.CommandText = "insert into EmpImg(EmpId,Ename,Eimage)values(@EmpId,@Ename,@Eimage)";
cmd.ExecuteNonQuery();
con.Close();
Label4.Text = "Data inserted";
BindGrid();
}
public void BindGrid()
{
SqlConnection con = new SqlConnection(sqlc);
SqlCommand cmd = new SqlCommand("select * from EmpImg", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
Add New Item
Write the following Code in Handler
using System;
using System.Web;
public class Handler : IHttpHandler {
public void ProcessRequest(HttpContext context)
{
string strcon = System.Configuration.ConfigurationManager.ConnectionStrings["SqlConnection"].ToString();
string imageid = context.Request.QueryString["ImID"];
System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(strcon);
connection.Open();
System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand("select Eimage from EmpImg where EmpId=" + imageid, connection);
System.Data.SqlClient.SqlDataReader dr = command.ExecuteReader();
dr.Read();
context.Response.BinaryWrite((Byte[])dr[0]);
connection.Close();
context.Response.End();
}
public bool IsReusable {
get {
return false;
}
}
}
Output:
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="EmpId"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" MaxLength="3"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="Ename"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server" Style="margin-top: 0px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text="Emp Image"></asp:Label>
</td>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:Button ID="Button1" runat="server" Text="Save" OnClick="Button1_Click" />
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label4" runat="server"></asp:Label>
</td>
</tr>
</table>
</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="FALSE">
<Columns>
<asp:BoundField HeaderText="EmpId" DataField="EmpId" />
<asp:BoundField HeaderText="Ename" DataField="Ename" />
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "Handler.ashx?ImID="+ Eval("EmpId") %>' Height="150px" Width="150px" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
.cs Page
string sqlc = System.Configuration.ConfigurationManager.ConnectionStrings["SqlConnection"].ToString();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(sqlc);
SqlCommand cmd=new SqlCommand();
Stream fs = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
con.Open();
cmd.Parameters.AddWithValue("@EmpId", Convert.ToInt32(TextBox1.Text));
cmd.Parameters.AddWithValue("@Ename", TextBox2.Text.ToString());
cmd.Parameters.Add("@Eimage", SqlDbType.Binary).Value = bytes;
cmd.Connection=con;
cmd.CommandText = "insert into EmpImg(EmpId,Ename,Eimage)values(@EmpId,@Ename,@Eimage)";
cmd.ExecuteNonQuery();
con.Close();
Label4.Text = "Data inserted";
BindGrid();
}
public void BindGrid()
{
SqlConnection con = new SqlConnection(sqlc);
SqlCommand cmd = new SqlCommand("select * from EmpImg", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
Add New Item
Write the following Code in Handler
using System;
using System.Web;
public class Handler : IHttpHandler {
public void ProcessRequest(HttpContext context)
{
string strcon = System.Configuration.ConfigurationManager.ConnectionStrings["SqlConnection"].ToString();
string imageid = context.Request.QueryString["ImID"];
System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(strcon);
connection.Open();
System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand("select Eimage from EmpImg where EmpId=" + imageid, connection);
System.Data.SqlClient.SqlDataReader dr = command.ExecuteReader();
dr.Read();
context.Response.BinaryWrite((Byte[])dr[0]);
connection.Close();
context.Response.End();
}
public bool IsReusable {
get {
return false;
}
}
}
Output:
No comments:
Post a Comment