Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

classic ASP query issue

Posted on 2014-01-16
8
Medium Priority
?
722 Views
Last Modified: 2014-01-16
Hi,

I have a classic ASP page where I was trying to pull in a text string of comma separated numbers, then pull the names for each numeric value from another db table, and display the text names in a comma separated list on the webpage.

I'm in over my head with the syntax. Is this a fairly routine piece of code?

So I have session variable: session("UserTypes") that would be something like 1,3,5,7.

And I have a db table called AccessTypes that has AccessTypeID and AccessName.

How would I turn 1,2,5,7 into AccessName1, AccessName2, AccessName5, and AccessName7 and write that value to html?

Thank you

Bill
0
Comment
Question by:billium99
[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
8 Comments
 
LVL 1

Author Comment

by:billium99
ID: 39786737
BTW - the connection is handled with this string:

recordset.ActiveConnection = MM_mande_STRING
0
 
LVL 33

Expert Comment

by:Big Monty
ID: 39786762
you would need to put the comma separated values into an array:

session("UserTypes") = 1,3,5,7

'-- split the values into an array
if Session("userTypes") <> "" then
    arrUserTypes = Split( Session("userTypes"), "," )

    '-- now loop through the array and save to the database
    for each userType in arrUserTypes
        '-- always good to do one final check if the value is blank or not
        if userType <> "" then
             sql = "insert into tblUserTypes ( userType ), values( " & CInt( userType ) & " )"
             conn.Execute( sql )
        end if
    next

end if

Open in new window


of course, you're better off doing a parameterized query, I did it this way for the sake of demonstration. There's no check on the actual value (if it's an int data type or not), but you should get the basic idea.
0
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39786781
This is what I would do

in the VBSCRIPT, replacing the comma with what ever you want including the comma at the start of it, will solve the issue...

the skeleton of the code is shown below, please convert it into the appropriate VB script syntax
 

1) declare a variable let us say Temp
2) Temp = "AccessName" + replace(temp,",",",AccessName")
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 33

Expert Comment

by:Big Monty
ID: 39786823
my apologies, I misread it as you wanted to save to the database, not read from it...

either way, the approach I gave should work, but let me ask you, are you trying to build some sql where it says

where AccessTypeID = 1 or AccessTypeID = 2 etc?

if so, why not just do

"where AccessTypeID in ( " & Session("UserTypes") & " ) "
0
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 39786871
If you're looking for a TSQL solution that you can use as a stored procedure...

DECLARE @UserTypes VARCHAR(MAX)
SET @UserTypes = '1, 2, 3, 7'

SELECT STUFF(
      (
            SELECT ',' + T.Name
            FROM dbo.SplitText(@UserTypes, ',') AS UT
            INNER JOIN SomeTable AS T
                  ON UT.Value = T.EntitlementTypeID
            FOR XML PATH('')
      ), 1, 1, '')

Note: This assumes you have or can create a table-valued split function
0
 
LVL 1

Author Comment

by:billium99
ID: 39787004
Thanks All,

Hmm so yes I'm reading from a database table and extrapolating actual text names for a comma delimited list of IDs. I'm hoping to display the text names is a similarly delimited list. This is the logged in user's session value for UserTypes. Here is the code I have attempted:

<%
			
				Set Usersr_cmd = Server.CreateObject ("ADODB.Command")
				Usersr_cmd.ActiveConnection = MM_mande_STRING
				Usersr_cmd.CommandText = "SELECT accessname FROM accesslevels where  AccessID = "& Session("UserTypes") &" ORDER BY accessname Asc" 
				Usersr_cmd.Prepared = true
				
				Set Usersr = Usersr_cmd.Execute
				accessname = ""
				if not Usersr.eof then
					do while not Usersr.eof
					accessname = accessname & Usersr("accessname") & ","
					Usersr.movenext
					loop
				else
					accessname = ""	
				end if
				Set Usersr = nothing
				Response.Write left(accessname,len(accessname)-1)
			
			%>

Open in new window


Now I'm getting a syntax error but I'm also not sure if I have this looping thing working as it would need to be...anyway, I'm getting:


Microsoft OLE DB Provider for SQL Server error '80040e14'

Incorrect syntax near ','.

/index.asp, line 702


Thanks for the time. Any ideas here?

Bill
0
 
LVL 33

Accepted Solution

by:
Big Monty earned 2000 total points
ID: 39787018
you're trying to compare the database field accessID (which I presume is a int value) to a string of numbers, separated by a comma...won't work :)

change

Usersr_cmd.CommandText = "SELECT accessname FROM accesslevels where  AccessID = "& Session("UserTypes") &" ORDER BY accessname Asc"
                        

to

Usersr_cmd.CommandText = "SELECT accessname FROM accesslevels where  AccessID in ("& Session("UserTypes") &") ORDER BY accessname Asc"
0
 
LVL 1

Author Closing Comment

by:billium99
ID: 39787054
Boom! That did it. You were right, Big Monty. Different data types...

Thanks for all the suggestions!

Bill
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

670 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