Go Back   CodingForums.com > :: Server side development > ASP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-07-2012, 07:55 PM   PM User | #1
robuc
New Coder

 
Join Date: Jan 2012
Posts: 17
Thanks: 8
Thanked 0 Times in 0 Posts
robuc is an unknown quantity at this point
Cool How do i DISPLAY a variable on an ASP page that was passed from a URL

Hi, I have situation here that works in Coldfusion bout does NOT work here in ASP.NET... Please HELP... How do i DISPLAY a variable on an ASP page that was passed from a URL, i.e.

1. URL and variable:
a) [<a href="search/ccSearch.aspx?countryName=mycountry&countryID=2">selected country</a>]

b) [OR, <a ID="3" href="search/ccSearch.aspx">country link</a>]

2. What would be the OUTPUT code on the asp.net page for the above?

x) say, for (a) above i wanted to display the country name on the on the destination page... <%# url.countryName %> [what would be the answer because that does not work... it would work for CFM but not here].

y) also, (a) or (b) above... if i wanted to use the IDs to pull data from a database how would i do that? For example: [SELECT * FROM myDatabase WHERE myDatabaseID=<%# URL.ID %>] would that work? OR "WHERE myDatabaseID=<%# URL.countryID %>"

thats it.... thanks for the help!!!
robuc is offline   Reply With Quote
Old 02-08-2012, 02:12 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,247
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Major question: Is this code supposed to appear *ON* the page named ccSearch.aspx? Or will it appear on some other (possibly HTML, not even ASP.NET) page?

If the former, then you need to stop what you are doing and go back and learn how to correctly use ASP.NET controls.

But if the latter...

First of all, THIS code would never work to send the ID in *ANY* system. ASP/JSP/PHP/ASP.NET or even CF:
Code:
<a ID="3" href="search/ccSearch.aspx">country link</a>
When you use an href to invoke *ANY* server-side page, the ONLY way to pass data is as part of the query string. So you *MUST* do something like
Code:
<a href="search/ccSearch.aspx?ID=3">country link</a>
(And in any case an HTML id="..." starting with a digit is not supported in many ways.)

For
Code:
<a href="search/ccSearch.aspx?countryName=mycountry&countryID=2">
you can use Request.QueryString("countryName") to retrieve the value in the query string.

So you could just use
Code:
<% Response.Write( Request.QueryString("countryName") ) %>
(needs a terminating semicolon of you are writing in C# instead of VB.NET).

But there are many many better ways to do this with ASP.NET. And the only way you are going to learn them is to start with a *GOOD* ASP.NET tutorial and work through all the examples.

Same, especially, applies to using a database with ASP.NET. There are so many ways to do so that I can't possibly go into all of them here.

The simplest would be
Code:
' VB.Net
Dim SQL As String = "SELECT * FROM myDatabase WHERE myDatabaseID=" & Request.QueryString("id")

' C#
String SQL = "SELECT * FROM myDatabase WHERE myDatabaseID=" + Request.QueryString("id");
But odds are that's not what you are going to ultimately want to do. Again, go find a tutorial and start working through *ALL* the examples.

ASP.NET is enormously flexible and yet, at the same time, there are some rigid rules that will help you use it in the best ways. ASP.NET has a steep learning curve, compared to CF or PHP, but the end result is worth it. But you must spend the time to REALLY learn it if you expect to get good results from it.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
robuc (02-08-2012)
Old 02-08-2012, 02:22 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,247
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
By the by....your question is posed wrong: You are NOT asking about "an ASP page". You are asking about "an ASP.NET page".

ASP and ASP.NET are vastly different, despite the similarity in names. Kind of like Java and JavaScript.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
robuc (02-08-2012)
Old 02-08-2012, 04:43 PM   PM User | #4
robuc
New Coder

 
Join Date: Jan 2012
Posts: 17
Thanks: 8
Thanked 0 Times in 0 Posts
robuc is an unknown quantity at this point
Cool

Old Pedant, i don't know if i should thank you or feel offended... anyway i am going to thank you, you did answer my questions .

I have never used Code:
Quote:
<a ID="3" href="search/ccSearch.aspx">country link</a>
I just wanted to cover all the bases.

But your other explanations helped... Thanks again!!! It actually worked, including the database query.
robuc is offline   Reply With Quote
Old 02-08-2012, 07:35 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,247
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
If my answer helped, I'm glad. I was more than a little worried that you were heading off in the wrong direction, as you can certainly tell. If it makes you feel any better, those of us who transitioned from ASP to ASP.NET went through the same learning curve. In some ways, the shock to our systems was bigger than yours has been, because there are a lot of things you *CAN* do in ASP.NET the same way we did them in ASP. Just that those are almost inevitably not the best ways from an ASP.NET-ish perspective.

I had to relearn almost everything I thought I knew, just as you are going through now. But, in the long run, if you take the time to learn the principles of ASP.NET and explore its "corners", you'll be able to produce much more efficient code.

So...no offense ever intended. Just concern that you understand that sometimes the quick and dirty way isn't the best way.

(The real killer for me was finding out that database connections are not closed automatically for you! ASP does that, so if--for example--you have an error in an ASP page you can safely dump out a message and then just stop With ASP.NET, you *must* catch the error, display the message, and then be absolutely sure you have closed all open connections. Else you will indeed run out of connections sooner than you might think!)
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
robuc (02-21-2012)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:10 AM.


Advertisement
Log in to turn off these ads.