ASP.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a ASP.NET Expert!

Join 300,579 ASP.NET Programmers for FREE! Get instant access to thousands of ASP.NET experts, tutorials, code snippets, and more! There are 2,225 people online right now. Registration is fast and FREE... Join Now!




Pagination

 

Pagination, Seems like SQL doesn't do it well?

Jediman

5 Sep, 2007 - 11:41 AM
Post #1

New D.I.C Head
*

Joined: 8 Aug, 2007
Posts: 10


My Contributions
Hi all!
I'm currently designing a link index/database along with a news/CMS system, and was trying to determine how to do a couple of things, mainly pagination...

I know you can enable pagination when using a datagrid, but datagrids IMHO are ugly as sin, and I prefer the old fasioned way of returning records by coding it myself. So that's what i've done. I've coded a system that will display categories, etc at the click of a link.
What i'd like to do, since SQL doesn't have the ability to limit the # of records returned by default (like MySQL does) and rather than use the top 10 or 20, which only pulls the stuff from the beginning of the DB on out....I'd like to set it up so I can do some pagination.

I've search around the web and couldn't exactly find a good, solid tutorial or information on really implementing this.
I did find one decent article, http://www.aprogrammersjournal.com/article.aspx?id=67, but there must be a simpler, easier method of doing the same thing...

I'm also using C# should that matter at all.

Basically, here is what I have written thus far that shoots out the results of a database used for links..

Basically the page initially displays the catagories available for this particular section from a master categories table, and then once a user clicks on one of the categories, goes and fetches it from another table accordingly.

CODE

SqlConnection con = null;
        SqlCommand cmd = null;
        SqlDataReader rd = null;
        
        try {
            con = new SqlConnection("server=myserver;uid=myusername;" +
                "pwd=password;database=mydatabase");
            cmd = new SqlCommand("SELECT * FROM resources WHERE mastercat ='Forms' AND cat ='"+Request.QueryString["cat"]+"'", con);
            
            con.Open();
            rd = cmd.ExecuteReader();
            
            while(rd.Read()) {
                Response.Write("<table border=1>");
                Response.Write("<tr><td> ");
                Response.Write(rd.GetString(6));
                Response.Write("</td></tr> ");
                Response.Write("<tr><td> ");
                Response.Write(rd.GetString(7));
                Response.Write("</td></tr> ");
                Response.Write("<tr><td><a href="+rd.GetString(5)+" target=_blank>");
                Response.Write("Download</a></td></tr> ");
                Response.Write("<br></table> ");
            }
        } catch (Exception e) {
            Response.Write("<p><font color=\"red\">Err: ");
            Response.Write(e.Message);
            Response.Write("</font></p>");
        } finally {
            if(rd != null)
                rd.Close();
            if(con != null)
                con.Close();
        }
      }


It's set up to get the name of the link, a description, and then the link itself.
What I want to do is paginate it, so that say only about 3-5 links will show at one time.
I haven't found a good way of doing this yet, and I'd like to do this, as I can carry it over to my news section as well...and that brings up my other question.

Currently, my news system just displays the news thats in a database. I have fields that desginate the author, title, intro, full body, time/date submitted. What is a good way to get just the latest news...e.g. if the news is older than today but there was no news posted sans maybe a week ago, just display that...? What is a good way to do that?

Thanks for any feedback!


User is offlineProfile CardPM
+Quote Post


girasquid

RE: Pagination

5 Sep, 2007 - 09:31 PM
Post #2

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,628



Thanked: 53 times
Dream Kudos: 825
My Contributions
I can't say that I know much about SQL(I use MySQL), but I did do some quick googling and find SQL Pagination -- the final solution.

The best way to get the 'latest' news would be to order it by something descending, and only select the top result - that way, the newest entry would be the only one returned. Something like this works under MySQL(I don't know if it would under SQL):
CODE

SELECT * FROM news ORDER BY date_posted DESC LIMIT 1;


Sorry I couldn't help more.
User is offlineProfile CardPM
+Quote Post

Jediman

RE: Pagination

6 Sep, 2007 - 09:43 AM
Post #3

New D.I.C Head
*

Joined: 8 Aug, 2007
Posts: 10


My Contributions
QUOTE(girasquid @ 5 Sep, 2007 - 10:31 PM) *

I can't say that I know much about SQL(I use MySQL), but I did do some quick googling and find SQL Pagination -- the final solution.

The best way to get the 'latest' news would be to order it by something descending, and only select the top result - that way, the newest entry would be the only one returned. Something like this works under MySQL(I don't know if it would under SQL):
CODE

SELECT * FROM news ORDER BY date_posted DESC LIMIT 1;


Sorry I couldn't help more.


The decending sort with selecting the top X works like a charm icon_up.gif

I can't seem to really get pagination working though outside of a nasty looking grid...ugh.

Isn't there some, decent way of doing this with SQL 2000? I'd upgrade to 2005 but it would break some other stuff here it seems.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 08:21AM

Live ASP.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

ASP.NET Tutorials

Reference Sheets

ASP.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month