Hi,
I Uploaded Images in DataList and saved those images in SqlServer using Handler.
Now i want to show paging.Please help me.
Default.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Image
<asp:Label ID="lblTags" runat="server" Text="Tags">
<asp:TextBox ID="txtTags" runat="server">
<br />
<asp:Label ID="lblImage" runat="server" Text="Upload Picture">
<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">
<br />
<hr />
<asp:DataList ID="DataList1" runat="server" RepeatColumns="3" RepeatLayout="Table"
RepeatDirection="Horizontal" DataKeyField="pic_id" DataSourceID="SqlDataSource1">
<itemtemplate>
<asp:Image ID="picAlbum" runat="server" AlternateText='<%Eval("Picture_tag") %>'
ImageUrl='<%# "ShowImage.ashx?id="+Eval("pic_id") %>' Height="100" Width="100" />
</itemtemplate>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:TurcoItalianaConnectionString %>"
SelectCommand="SELECT [pic_id], [picture_tag], [pic] FROM [Images1]" >
</div>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public 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["TurcoItalianaConnectionString"].ConnectionString;
connection = new SqlConnection(conn);
connection.Open();
string sql = "INSERT INTO Images1(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);
DataList1.DataBind();
}
catch
{
lblResult.Text = "There was an error";
}
finally
{
connection.Close();
}
}
}
ShowImage.ashx
<%@ WebHandler Language="C#" Class="ShowImage" %>
using System;
using System.Web;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
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.Request.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["TurcoItalianaConnectionString"].ConnectionString;
SqlConnection connection = new SqlConnection(conn);
string sql = "SELECT pic FROM Images1 WHERE Pic_ID = @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;
}
}
}
This is my code now i want to add Paging i tried so many solutions but it does not work that's why i'm asking