Click here to Skip to main content
15,892,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)
            {
                //To create a PostedFile
                HttpPostedFile File = imgUpload.PostedFile;
                //Create byte Array with file len
                imgByte = new Byte[File.ContentLength];
                //force the control to load data in array
                File.InputStream.Read(imgByte, 0, File.ContentLength);
            }
            // Insert the picture tag and image into db
            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) 
    {
        //context.Response.ContentType = "text/plain";
        //context.Response.Write("Hello World");
        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
Posted
Updated 15-Dec-10 19:16pm
v3
Comments
Vigneshb6 15-Dec-10 7:59am    
Use PagedDataSource
Toniyo Jackson 16-Dec-10 1:16am    
Always write your code inside code block

I think this code helps you...............

protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           FetchData(50, 0);
       }
       else
       {
           place1.Controls.Clear();
           CreatePagingControl();
       }
   }


private int RowCount
  {
      get { return Convert.ToInt32(ViewState["RowCount"]); }
      set { ViewState["RowCount"] = value; }
  }

private void FetchData(int take, int pageSize)
  {



           if (ds.tTables[0].Row.count-1>0)
           {
               Response.Write("");
           }
           else
           {

             //your sql query

               PagedDataSource page = new PagedDataSource();
               page.AllowCustomPaging = true;
               page.AllowPaging = true;
               page.DataSource = query;
               page.PageSize = 50;
               DataList1.DataSource = page;
               DataList1.DataBind();
               if (!IsPostBack)
               {
                   RowCount = query.First().Count;
                   CreatePagingControl();
               }
           }
      }
 private void CreatePagingControl()
   {
       for (int i = 0; i < (RowCount / 50) + 1; i++)
       {
           LinkButton lnk = new LinkButton();
           lnk.Click += new EventHandler(lbl_Click);
           lnk.ID = "lnkPage" + (i + 1).ToString();
           lnk.Text = (i + 1).ToString();
           place1.Controls.Add(lnk);
           Label spacer = new Label();
           spacer.Text = "&nbsp;";

           place1.Controls.Add(spacer);
       }
   }
   void lbl_Click(object sender, EventArgs e)
   {
       LinkButton lnk = sender as LinkButton;
       int currentPage = int.Parse(lnk.Text);
       int take = currentPage * 50;
       int skip = currentPage == 1 ? 0 : take - 50;
       FetchData(take, skip);
   }





XML
<asp:DataList ID="DataList1" runat="server" width="100%" >
    <ItemTemplate>
        <tr>
            <td class="account_box" >
                <table width="96%" align="center" cellpadding="0" cellspacing="0">
                  <tr>
                    <td height="10"></td>
                  </tr>
                  <tr>
                       <td height="30" bgcolor="#1DB0E6" class="signin" style="padding-left:15px;">
                             <table width="100%" cellspacing="0" cellpadding="0">
                                 <tr>
                                       <td class="signin" align="center"><%# Eval("Title")%></td>
                                </tr>
                            </table>
                       </td>
                </tr>
                <tr>
                      <td>&nbsp;</td>
                </tr>
                <tr>
                         <td>
                         <table width="100%" cellspacing="0" cellpadding="0">
                <tr>
                    <td width="80%" valign="top">
                         <table width="100%" cellspacing="0" cellpadding="0">
                          <tr>
                               <td height="22" class="pws">Description:&nbsp;&nbsp;<asp:Label ID="lb_description" runat="server" Text='<%# Eval("Description") %>'></asp:Label></td>
                          </tr>
                          <tr>
                             <td height="22">Keyword: &nbsp;&nbsp;<asp:Label ID="Label1" runat="server" Text='<%# Eval("Keyword") %>'></asp:Label></td>
                         </tr>
                       <tr>
                           <td height="22" style="font-size:11px; color:#666666;">
                               Contact Person Name:&nbsp;&nbsp;<asp:Label ID="lb_contact" runat="server" Text='<%# Eval("ContactName") %>'> </asp:Label><br />
                               Email:&nbsp;&nbsp;<asp:Label ID ="lb_email" runat ="server" Text ='<%# Eval("Email") %>'></asp:Label></td>
                      </tr>
                      <tr>
                        <td height="22" style="font-size:11px;">Company Website:&nbsp;&nbsp;<%# Eval("Url")%></td>
                      </tr>
                    </table>
                    </td>
                  </tr>
                </table>
                </td>
              </tr>
              <tr>
               <td height="10"></td>
              </tr>
            </table></td>
          </tr>
         <tr>
            <td>&nbsp;</td>
          </tr>
         </tr>
       </ItemTemplate>
            </asp:DataList>
           </tr>
            </table>
             <table>
           <tr>
            <td>
                <asp:PlaceHolder ID="place1" runat="server" />
                <br /><asp:Label runat="server" ID="lblPageName" />
            </td>
        </tr>
    </table>
 
Share this answer
 
v2
What about search engines? I just put your question title in Google[^]

In first I got the following links

Paging Report Data in a DataList or Repeater Control[^]

Efficient Data Paging with the ASP.NET 2.0 DataList Control and ObjectDataSource [^]

Paging in DataList[^]

DataList Custom Paging in ASP.NET using C#[^]

.
.
.
.
35,500 results

Here after search in search engines before asking here. Also come with your code snippets so that it will help you to get solutions quickly.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900