Here is my code, plz see where is error:
protected void btnSubmit_Click(object sender, EventArgs e)
{
SqlConnection connection = null;
try
{
FileUpload img = (FileUpload)imgUpload;
Byte[] imgByte = null;
if (img.HasFile && img.PostedFile != null)
{
HttpPostedFile File = imgUpload.PostedFile;
imgByte = new Byte[File.ContentLength];
File.InputStream.Read(imgByte, 0, File.ContentLength);
}
string conn = ConfigurationManager.ConnectionStrings["ProductConnectionString1"].ConnectionString;
connection = new SqlConnection(conn);
connection.Open();
string sql = "INSERT INTO Album(Picture_Tag,Pic) VALUES(@tag,@pic) SELECT @@IDENTITY";
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.Parameters.AddWithValue("@tag", txtTags.Text);
cmd.Parameters.AddWithValue("@pic", imgByte);
int id = Convert.ToInt32(cmd.ExecuteScalar());
lblResult.Text = String.Format("Picture ID is {0}", id);
ListView1.DataBind();
}
catch
{
lblResult.Text = "There was an error";
}
finally
{
connection.Close();
}
}
This is .aspx page:
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div>
Image
<asp:Label ID="lblTags" runat="server" Text="Tags"></asp:Label>
<asp:TextBox ID="txtTags" runat="server" Width="453px"></asp:TextBox>
<br />
<asp:Label ID="lblImage" runat="server" Text="Upload Picture"></asp:Label>
<asp:FileUpload ID="imgUpload" runat="server" />
<br />
<br />
<asp:Button ID="btnSubmit" runat="server" onclick="btnSubmit_Click"
Text="Submit" />
 <asp:Label ID="lblResult" runat="server" ForeColor="#0066FF"></asp:Label>
<br />
<hr />
</div>
<asp:ListView ID="ListView1" GroupItemCount="4" runat="server" DataKeyNames="PICID" DataSourceID="SqlDataSource1">
<LayoutTemplate>
<asp:Placeholder
id="groupPlaceholder"
runat="server" />
</LayoutTemplate>
<GroupTemplate>
<div>
<asp:Placeholder
id="itemPlaceholder"
runat="server" />
</div>
</GroupTemplate>
<ItemTemplate>
<asp:Image id="picAlbum" runat="server" AlternateText='<% #Eval("Picture_Tag") %>' ImageUrl='<% #"ShowImage.ashx?id=" + Eval("PICID") %>' />
</ItemTemplate>
<EmptyItemTemplate>
</EmptyItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ProductConnectionString1 %>"
ProviderName="System.Data.SqlClient"
SelectCommand="SELECT [PICID],Picture_Tag, [Pic] FROM [Album]"></asp:SqlDataSource>
</asp:Content>
and this is my handler class:
<%@ WebHandler Language="C#" Class="ShowImage" %>
using System;
using System.Configuration;
using System.Web;
using System.IO;
using System.Data;
using System.Data.SqlClient;
public class ShowImage : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
Int32 picid;
if (context.Request.QueryString["id"] != null)
picid = Convert.ToInt32(context.Request.QueryString["id"]);
else
throw new ArgumentException("No parameter specified");
context.Response.ContentType = "image/jpeg";
Stream strm = ShowAlbumImage(picid);
byte[] buffer = new byte[4096];
int byteSeq = strm.Read(buffer, 0, 4096);
while (byteSeq > 0)
{
context.Response.OutputStream.Write(buffer, 0, byteSeq);
byteSeq = strm.Read(buffer, 0, 4096);
}
}
public Stream ShowAlbumImage(int picid)
{
string conn = ConfigurationManager.ConnectionStrings["ProductConnectionString1"].ConnectionString;
SqlConnection connection = new SqlConnection(conn);
string sql = "SELECT Pic FROM Album WHERE PICID = @ID";
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@ID", picid);
connection.Open();
object img = cmd.ExecuteScalar();
try
{
return new MemoryStream((byte[])img);
}
catch
{
return null;
}
finally
{
connection.Close();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
here i am getting Alt text fine but when i call handler class through Asp:Image tag then there is no result. And no image is showing on page.