Wrox Programmer Forums
|
ASP.NET 4.5 General Discussion For ASP.NET 4.5 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4.5 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 9th, 2013, 03:49 PM
Registered User
 
Join Date: Apr 2013
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Hi Imar,

I do. I was just saying that creating a daily randomised extract would have been a solution I could have set up within a day. That would have been a compromise.

However, by pushing to find a solution to (what initially appeared to be) a simple problem, I'm learning more new stuff rather than relying on my SQL knowledge.

Feel at home with VS now, having played with it for a day or so. Now to copy my website locally and play with that to see how it looks under VS. I'll also try migrating it to 4.5 and see what changes. My hosting company has recently extended support to 4.5, so I may as well as move to that too.

Next, to apply your EF approach to my problem - one question already: I assume I'll need to use Linq to retrieve the data. Can I use 'NEWID()' - or indeed any SQL function - in the 'ORDER BY' clause? If not, how do you see me randomising the results?

Phil.
 
Old May 10th, 2013, 04:00 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Phil,

Quote:
I do. I was just saying that creating a daily randomised extract would have been a solution I could have set up within a day. That would have been a compromise.
Agreed. Just saying that you can still do that and access that data from EF. Not as much fun though ;-)

Quote:
However, by pushing to find a solution to (what initially appeared to be) a simple problem, I'm learning more new stuff rather than relying on my SQL knowledge.
Agreed again. Best way to learn stuff ;-)

Quote:
Can I use 'NEWID()' - or indeed any SQL function - in the 'ORDER BY' clause?
It seems it's easy to do using Guid.NewGuid:
Code:
from e in MyEntities orderby Guid.NewGuid() select e
Details can be found here: http://stackoverflow.com/questions/6...s-random-order
Note, some people reported this doesn't work. I think it depends on the version of EF you're using.I just tried it myself and it works for me; it generates a set of randomized records and executes it at the database server level. That's especially important if you have a large dataset and you're using something like Take(10) to only bring in the actual records you need.

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
pjskeldon (May 10th, 2013)
 
Old May 10th, 2013, 01:39 PM
Registered User
 
Join Date: Apr 2013
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thanks Imar.

As usual, just when I start to flounder, you (or one of your books) comes to the rescue.

I think I'm getting close to wrapping this up over the weekend.

Phil.
 
Old June 17th, 2013, 01:40 PM
Registered User
 
Join Date: Apr 2013
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Imar,

I’m stumped.

Admittedly it took me some time to absorb VSEW 2012 and EF (and I’m still a little wobbly), but I feel that although I’m adding more sophisticated code, the problems seem to be multiplying. I feel I’m missing something fundamental here.

I do mean that I’m at fault here, not the suggestions I’ve received from you and others so far.

At its most basic, here’s the relevant mark-up (all other lines removed). Conceptually I’m retrieving the data from one SQL Server table into a local table and binding it to the ListView during a non-pageback Page_Load subroutine.
Code:
<script type="text/vb" runat="server">
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

        Me.MaintainScrollPositionOnPostBack = "True"

        If Not Page.IsPostBack Then
            Dim MyConnection As SqlConnection
            Dim MyCommand As SqlCommand
            Dim MyDataTable As DataTable
            Dim MyReader As SqlDataReader

            MyConnection = New SqlConnection()
            MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString

            MyCommand = New SqlCommand()
            MyCommand.CommandText = "SELECT [HTMLSrc],[HTMLTitle] FROM [BusinessAdverts] " & _
                                        "WHERE [ValidFrom] <= GETDATE() AND " & _
                                        "[ValidTo] >= GETDATE()" & _
                                        "ORDER BY NEWID()"
										
            MyCommand.CommandType = CommandType.Text
            MyCommand.Connection = MyConnection

            MyCommand.Connection.Open()
            MyReader = MyCommand.ExecuteReader(CommandBehavior.CloseConnection)

            MyDataTable = New DataTable()

            ' Loading DataTable using a DataReader
            MyDataTable.Load(MyReader)

            lvAdverts.DataSource = MyDataTable
            lvAdverts.DataBind()

            MyDataTable.Dispose()
            MyCommand.Dispose()
            MyConnection.Dispose()
        End If


    End Sub

	<asp:ListView id="lvAdverts" 
					runat="server" 
					>

		<EmptyDataTemplate>
			<p>No adverts to show at the moment</p>
		</EmptyDataTemplate>

		<ItemSeparatorTemplate>
            <br />
		</ItemSeparatorTemplate>

		<ItemTemplate>
	
			<asp:Label id="HTMLtext" runat="server" Text='<%# "<div><a href=""images/" + Eval("HTMLSrc") + "_banner.jpg""><img src=""images/" + Eval("HTMLSrc") + "_banner_thumb.jpg"" alt="""" title=""" + Eval("HTMLTitle") + """ /></a></div>"%>' />

		</ItemTemplate>

		<LayoutTemplate>
			<div id="itemPlaceholderContainer" runat="server" style="">
				<span runat="server" id="itemPlaceholder" />
			</div>
		</LayoutTemplate>
		
	</asp:ListView>

 
 <br />
				
		<asp:DataPager runat="server" id="DataPager1" PageSize="3" PagedControlID="lvAdverts">
			<Fields>
				<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowLastPageButton="True" />
			</Fields>
		</asp:DataPager>
The effect is that the first three adverts (cut down to 3 to make testing and changing faster) with four buttons – First Previous, Next and Last. The first two are greyed-out, while the next two are active.

However, clicking on the Next button appears to do nothing. The same 3 adverts show and the buttons retain their greyed-out/active status.

Clicking again on the Next button makes a small change. The same 3 adverts show but all the buttons are now active. Remember there are about 60 adverts in total.

Actually clicking on any active buttons makes no difference to the adverts shown.

I trawled the ‘net and came across some advice on re-binding the table to the control and tried this, but I had difficulty accessing the table as it had moved out of scope (disposed in the non-pageback Page_Load sub).

As I say, while I have gone further and set up EF solutions, the result is essentially the same.

To be honest I’ve left this alone for a week or so as it was doing my head in.

Before I try again, can you say why this most basic approach fails and what EF offers to solve it – to my mind EF’s strength is conceptually decoupling the actual database (SQL Server or whatever) from the code. I don’t see how it changes the nature of the persistent ‘temporary’ table extracted from the SQL Server DB and used to page back and forth in the ListView using the DataPager.

I know this may come across as personal. It is not. I honestly feel I’m missing the point here and felt it would be best to explain what I’ve tried, what the effects are and where the holes in my knowledge are.

Help me Obi-Wan Kenobi – you’re my only hope!

Phil.
 
Old June 24th, 2013, 02:52 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

>> Before I try again, can you say why this most basic approach fails and what EF offers to solve it

EF makes things like paging (using Skip and Take) easier. However, many of the concepts (controlling the PagerBar) would be similar and EF wouldn't make this much easier.

Do you still need help with this? I can cook up an example using EF for you somewhere this week or so.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old September 13th, 2013, 03:40 AM
Registered User
 
Join Date: Jun 2013
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default RE:

Hello....I am fresher for ASP .NET and I like to learn more about paging random data. I request you to please share more information about this.

Thanks...





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 21: Try It Out: Writing Data to Random Access File malie22001 BOOK: Beginning Visual C# 2010 2 July 16th, 2012 03:26 AM
data paging rahulbedardi ASP.NET 4 General Discussion 0 March 19th, 2012 05:52 AM
Getting Data from a VB5 Random File lowell Visual Basic 2005 Basics 11 July 5th, 2007 05:39 AM
paging through data - Data Access Objects fedoracore PHP Databases 0 November 28th, 2006 07:54 PM
Help: dropdownlist posting back gives random data vickeyurs ADO.NET 5 September 3rd, 2005 09:28 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.