Solved

Need to do an SQL select statement in Classic ASP for a variable

Posted on 2012-03-22
11
299 Views
Last Modified: 2012-06-21
I need to replace a session variable with the results of an SQL statement
that statement essentially is:
SELECT EmailAddress FROM USERS WHERE UserID=session(userid)"

Open in new window


My code is currently:
dim paypalnew as string = "SELECT EmailAddress FROM USERS WHERE UserID='"&session(userid)&"'"	
session("paypalemail")=paypalnew

Open in new window


However I am getting an error at "as"

I tried doing paypalnew=thestring but it put it as straight text instead of being a SQL Statement.

This is my first real programming project, so i know that i am probably just missing some basics, but finding resources for classic ASP is very tough :/
0
Comment
Question by:dashifire
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 37753965
There are no datatypes in VBA other then variants so all variables are just defined like

Dim myVariableName

and to set a value to it :

Set myVariableName = 'whatever'

so your code would become :

dim paypalnew 
set paypalnew = "SELECT EmailAddress FROM USERS WHERE UserID='"&session(userid)&"'"	
session("paypalemail")=paypalnew

Open in new window


However you code will not achieve you desired result (as I understand it) of populating the session variable with the result of the query it will just populate it with the text.
0
 

Author Comment

by:dashifire
ID: 37753989
that's what i need to know how to do, get the variable
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 37753997
Here is a good asp resource :  http://www.asptutorial.info/

Here is a resource for learning about how to use ADO for connecting to databases using asp :

http://www.w3schools.com/ado/default.asp
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 29

Expert Comment

by:Paul Jackson
ID: 37754000
What kind of database are you using?
0
 

Author Comment

by:dashifire
ID: 37754003
MS SQL
0
 

Author Comment

by:dashifire
ID: 37754046
sweet resources btw.
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 37754073
Another nice tutorial here on how to use ado with asp to connect to a sql server database :

http://aspalliance.com/655_Basic_ADO_and_SQL_Tutorial.1
0
 
LVL 6

Accepted Solution

by:
worthyking1 earned 500 total points
ID: 37760824
In VB you do not have to dim your vars unless you have Option Explicit ON. Personally I never use it. I just pay attention to my vars and make sure I don't use the same name twice.

Not sure how much code you have versus what you're showing us, but you're missing some  basics so here's how I would do it...

SQL_Conn_STRING = "Driver={SQL Server};Server=MyServerName;Database=MyDBName;uid=MyUsername;pwd=MyPassword"
Set Con = Server.CreateObject("Adodb.Connection")
Con.Open SQL_Conn_STRING

set rs  = Con.Execute("SELECT Top1 EmailAddress FROM USERS WHERE UserID='"&session("userid")&"'")	
If NOT rs.EOF then
      paypalnew = Trim(rs("EmailAddress"))
      session("paypalemail")=paypalnew
Else
      ' No user found with this UserID
End if

Open in new window

0
 

Author Comment

by:dashifire
ID: 37760926
Thanks when I get a shot next I will try this. I do have option explicit on though, not my idea, but working with it anyway.
0
 
LVL 6

Expert Comment

by:worthyking1
ID: 37761160
Then just make sure to dim all your variables first before you use them.
0
 

Author Comment

by:dashifire
ID: 37772185
That was perfect, the tutorials are terrific, but they don't tell me what is a variable and i was getting confused. I got it up and running now, so it is all good! Thanks Guys!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Passing Parameter to Stored Procedure 4 41
ASP server side get value 15 50
Table header must be on top 2 58
Compress Newid value ms sql Mssql 4 48
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question