In Html Code:
<asp:GridView ID="gridAdayPreprcircltimactiv" runat="server" AutoGenerateColumns="False"
onselectedindexchanged="gridAdayPreprcircltimactiv_SelectedIndexChanged">
<columns>
<asp:BoundField DataField="intAdayPreprcircltimactivId" HeaderText="AdayPreprcircltimactivId"
InsertVisible="False" ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="strAdayPreprcircltimactivText" HeaderText="AdayPreprcircltimactivText"
InsertVisible="False" ReadOnly="True" SortExpression="ID" />
<asp:TemplateField HeaderText="Image">
<itemtemplate>
<asp:Image ID="Image1" runat="server" Height="28px"
ImageUrl='<%# "HandlerAdayPreprcircltimactiv.ashx?intAdayPreprcircltimactivId=" + Eval("intAdayPreprcircltimactivId")%>' Width="23px" />
</itemtemplate>
<asp:CommandField HeaderText="Select" ShowSelectButton="True" />
</columns>
in Save Button Code:
protected void btnsave_Click(object sender, EventArgs e)
{
FileUpload img = (FileUpload)upload_file;
Byte[] imgByte = null;
if (upload_file.HasFile && img.PostedFile != null)
{
HttpPostedFile File = upload_file.PostedFile;
imgByte = new Byte[File.ContentLength];
File.InputStream.Read(imgByte, 0, File.ContentLength);
}
string sql = "INSERT INTO DPSAdayPreprcircltimactiv(intAdayPreprcircltimactivId,strAdayPreprcircltimactivText,strImageurl) VALUES(@intAdayPreprcircltimactivId,@strAdayPreprcircltimactivText,@strImageurl)";
SqlCommand cmd = new SqlCommand(sql, (SqlConnection)Application.Get("DPS"));
cmd.Parameters.AddWithValue("@intAdayPreprcircltimactivId", txtAdayPreprcircltimactivid.Text.Trim());
cmd.Parameters.AddWithValue("@strAdayPreprcircltimactivText", txtAdayPreprcircltimactiv.Text.Trim());
cmd.Parameters.AddWithValue("@strImageurl", imgByte);
cmd.ExecuteNonQuery();
load_dgitems();
}
in Update Button:
protected void btnupdate_Click(object sender, EventArgs e)
{
FileUpload img = (FileUpload)upload_file;
Byte[] imgByte = null;
if (upload_file.HasFile && img.PostedFile != null)
{
HttpPostedFile File = upload_file.PostedFile;
imgByte = new Byte[File.ContentLength];
File.InputStream.Read(imgByte, 0, File.ContentLength);
}
string sql = "update DPSAdayPreprcircltimactiv set strAdayPreprcircltimactivText=@strAdayPreprcircltimactivText,strImageurl=@strImageurl where intAdayPreprcircltimactivId=@intAdayPreprcircltimactivId";
SqlCommand cmd = new SqlCommand(sql, (SqlConnection)Application.Get("DPS"));
cmd.Parameters.AddWithValue("@strAdayPreprcircltimactivText", txtAdayPreprcircltimactiv.Text.Trim());
cmd.Parameters.AddWithValue("@strImageurl", imgByte);
cmd.Parameters.AddWithValue("@intAdayPreprcircltimactivId", txtAdayPreprcircltimactivid.Text.Trim());
cmd.ExecuteNonQuery();
load_dgitems();
}
In Handle You Write this Code:
<%@ WebHandler Language="C#" Class="HandlerAdayPreprcircltimactiv" %>
using System;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
public class HandlerAdayPreprcircltimactiv : IHttpHandler {
public void ProcessRequest (HttpContext context)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Select intAdayPreprcircltimactivId,strAdayPreprcircltimactivText,strImageurl from DPSAdayPreprcircltimactiv where intAdayPreprcircltimactivId =@intAdayPreprcircltimactivId";
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = con;
SqlParameter ImageID = new SqlParameter("@intAdayPreprcircltimactivId", System.Data.SqlDbType.Int);
ImageID.Value = context.Request.QueryString["intAdayPreprcircltimactivId"];
cmd.Parameters.Add(ImageID);
con.Open();
SqlDataReader dReader = cmd.ExecuteReader();
dReader.Read();
context.Response.BinaryWrite((byte[])dReader["strImageurl"]);
dReader.Close();
con.Close();
}
public bool IsReusable {
get {
return false;
}
}
}
Try this Functionality.
Regards,
Anilkumar.D