Showing posts with label blundering. Show all posts
Showing posts with label blundering. Show all posts

Friday, March 23, 2012

Retrieving the First N Records from a SQL Query in VS 2005

Hi,

first off, I'm a TOTAL novice at this stuff, I'm just currently blundering my way through a complex site to learn stuff.

I'm trying to call the newest addition to a SQL database into a webpage, in this case, it'll be 'newest user', one result only. I've done several other data retrival sections using a datatable, but the guy who was helping me though it is unavailable at the moment and I get the feeling I've jumped into the deepend slightly.

Could anyone give me an example of how retrieving the First N Records from SQL should look in VS? Does it need to be in a data table or can it go in a label?

Sorry if this is somewhat vague, but as I said, I've really only been using VS for a week!

SELECT TOP 1 FROM [TableName] ORDER BY timestamp

Hope this helps

Al

|||

Hi,

Using the Top keyword is the solution. But the use of TOP has changed between SQL2K and SQL2K5.

In SQL2k you were not able to use TOP @.N which means you can not dynamically set the record count for "TOP"

Check the article for TOP @.N usage athttp://kodyaz.com/articles/article.aspx?articleid=2

Also have a look at the new t-sql functions named "Window Functions". Also you can find some samples athttp://kodyaz.com/articles/article.aspx?articleid=19

Eralper

http://www.eralper.com

|||

Cheers Al,

trouble is, I'm unsure as to where it needs to go! As I said, I'm pretty rubbish at this VS lark so far.

Should I be using a datatable to display the information, or because its only a single result I want pulling back, can it be done in a label?

Currently, I have this working to bring back a compleate list of users, but on another page, I need to bring back only the newest. Once again, sorry if I appear dense, learning curve and all that;


CODE:

Dim SiteuserAsNew mySite.Siteuser

dlDataList.DataSource = Siteuser.List

dlDataList.DataBind()

HTML:

<asp:DataListID="dlDataList"runat="server">

<ItemTemplate>

<asp:LabelID="HyperLink1"runat="server"NavigateUrl='<%#Eval("SiteuserID", "viewuser.aspx?SiteuserID={0}" ) %>'Text='<%#Eval("title") %>'Font-Italic="true"></asp:Label><asp:HyperLinkID="hlkUser"runat="server"NavigateUrl='<%#Eval("SiteuserID", "viewuser.aspx?SiteuserID={0}" ) %>'Text='<%#Eval("fullname") %>'></asp:HyperLink></tr>

</ItemTemplate>

</asp:DataList>

sql