I have a simple bit of code that is filling a select object from a db table. I want to check a value to set an entry as selected, but when I check a db value, I get an empty select box. This code produces the empty box:
response.write "<td><select name='FromDept'>"
Do While not rs.eof
If rs("DeptID") = 61 Then
response.write "<option value=" & rs("DeptID") & " selected>" & rs("DeptName") & "</option>"
Else
response.write "<option value=" & rs("DeptID") & ">" & rs("DeptName") & "</option>"
End If
rs.MoveNext
Loop
rs.close
response.write "</select></td>"
However, this code produces a select box with values:
response.write "<td><select name='FromDept'>"
LpCnt = 0
Do While not rs.eof
If LpCnt = 9 Then
response.write "<option value=" & rs("DeptID") & " selected>" & rs("DeptName") & "</option>"
Else
response.write "<option value=" & rs("DeptID") & ">" & rs("DeptName") & "</option>"
End If
rs.MoveNext
LpCnt = LpCnt + 1
Loop
rs.close
response.write "</select></td>"
Thanks for any help!
Assign the value to a temporary variable:
response.write "<td><select name='FromDept'>"
Do While not rs.eof
dept = rs("DeptID")
If dept = 61 Then
response.write "<option value=" & dept & " selected>" & rs("DeptName") & "</option>"
...
Empty drop down means you get error in that statement and it's ignored most likely due to On Error Resume Next line you have somewhere.
First of all, get rid of that On Error Resume Next line.
Second, error in such code means type conversion problem. Try this code instead:
Dim curDeptID
Do While not rs.eof
curDeptID = 0
If Not IsNull(rs("DeptID")) Then
curDeptID = CLng(CStr(rs("DeptID")))
End If
If curDeptID=61 Then
response.write "<option value=" & rs("DeptID") & " selected>" & rs("DeptName") & "</option>"
Else
response.write "<option value=" & rs("DeptID") & ">" & rs("DeptName") & "</option>"
End If
rs.MoveNext
Loop
Related
Just a little background, our marketing department has been using static weather images on their signage and asked if it would be possible to pull something that updated in realtime. Since I'm the only person in our department with any type of programming experience, and I'm a novice at best, I got asked to come up with a solution. I have worked with classic asp in the past for a couple of other small projects I've done, so I decided to go that route. I have everything working the way I want it to with the exception of the extended forecast data. In the sample below I have the variables that I want to display with a For statement to pull the extended forecast xml data for each day. But for some reason it is only pulling the the last day of the extended forecast.
If (objXMLDOM.parseError.errorCode <> 0) Then
Response.Write("<p>Error parsing XML: " & objXMLDOM.parseError.reason & "</p>")
Else
For i = 0 to 3
Set objFuture = objXMLDOM.getElementsByTagName("forecast_conditions").Item(i) 'pull the XML node for each from one to three
For Each xmlNode In objFuture.childNodes 'loop through the dom tree
If (xmlNode.nodeName = "icon") Then
strIcon1 = "<img src=""http://www.google.com" & xmlNode.Attributes.getNamedItem("data").text & """ border=""0"">" & vbCrLf
End If
If (xmlNode.nodeName = "condition") Then
strCondition1 = xmlNode.Attributes.getNamedItem("data").text & vbCrLf
End If
If (xmlNode.nodeName = "low") Then
strLow = xmlNode.Attributes.getNamedItem("data").text & vbCrLf
End If
If (xmlNode.nodeName = "high") Then
strHigh = xmlNode.Attributes.getNamedItem("data").text & vbCrLf
End If
If (xmlNode.nodeName = "day_of_week") Then
strDay = xmlNode.Attributes.getNamedItem("data").text & vbCrLf
End If
Set objFuture = nothing
Next
Next
End if
I am then outputing the 4 day forecast into my main page with the following code:
<%For Each Item in objXMLDOM.getElementsByTagName("forecast_conditions")
Response.Write ("<td>" & strDay & "<br>" & strIcon1 & "<br>" & strCondition1 & "<br>" & "Low: " & strLow & "°F" & "<br>" & "High: " & strHigh & "°F" & "<br>" & "</td>")
Next %>
I have been looking at this off and on for a couple of days now and just can't seem to find what I am missing to pull each day instead of the just the last one. If anyone has any suggestions I would appreciate it!
You are using the same set of variables to store the forecast data for each day. So during every iteration of your for loop you are overwriting the previous day's information.
The solution is to move the code that writes the data to the screen insideyour first for loop, like such:
If (objXMLDOM.parseError.errorCode <> 0) Then
Response.Write("<p>Error parsing XML: " & objXMLDOM.parseError.reason & "</p>")
Else
For i = 0 to 3
Set objFuture = objXMLDOM.getElementsByTagName("forecast_conditions").Item(i)
For Each xmlNode In objFuture.childNodes
...
Next
' Write to the page here
Response.Write ("<td>" & strDay & "<br>" & strIcon1 & "<br>" & strCondition1 & "<br>" & "Low: " & strLow & "°F" & "<br>" & "High: " & strHigh & "°F" & "<br>" & "</td>")
Next
End if
Following is the ASP code that I am working on:
Dim sConnString, connection, sSQL, backgroundColor
sSQL = "SELECT firstName, lastName, email, zip, country, company, industry, revenue, timestamp FROM users"
sConnString=//a connection string
Set connection = Server.CreateObject("ADODB.Connection")
connection.Mode=adModeRead
connection.Open(sConnString)
connection.execute(sSQL)
Response.Write"<table>"
do while not connection.EOF
if(backgroundColor="#f1f1f1")then
backgroundColor="#ffffff"
else
backgroundColor="#f1f1f1"
end if
response.write"<tr backgroundColor="& backgroundColor & "></td>" & connection("firstName") & "</td><td>" & connection("lastName") & "</td><td>" & connection("email") & "</td><td>" & connection("zip") & "</td><td>" & connection("country") & "</td><td>" & connection("company") & "</td><td>" & connection("industry") & "</td><td>" & connection("revenue") & "</td><td>" & connection("timestamp") & "</td></tr>"
connection.MoveNext
Loop
Response.Write"</table>"
connection.Close
Set connection = Nothing
I am getting the following error:
ADODB.Connection error '800a0bb9'
Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.
/wsu.asp, line 13
Line 13:
do while not connection.EOF
Could you please help me with this?
Updated code:
<html>
<body>
<%
Dim sConnString, connection, sSQL, backgroundColor
sSQL = "SELECT firstName, lastName, email, zip, country, company, industry, revenue, timestamp FROM users"
sConnString="a connection string for MS SQL database;"
Set connection = Server.CreateObject("ADODB.Connection")
Set rs = Server.CreateObject("ADODB.Recordset")
connection.Mode=adModeRead
rs.open sSQL,connection
Response.Write"<table>"
do while not connection.EOF
if(backgroundColor="#f1f1f1")then
backgroundColor="#ffffff"
else
backgroundColor="#f1f1f1"
end if
response.write "<tr backgroundColor="& backgroundColor & "></td>" & connection("firstName") & "</td><td>" & connection("lastName") & "</td><td>" & connection("email") & "</td><td>" & connection("zip") & "</td><td>" & connection("country") & "</td><td>" & connection("company") & "</td><td>" & connection("industry") & "</td><td>" & connection("revenue") & "</td><td>" & connection("timestamp") & "</td></tr>"
connection.MoveNext
Loop
Response.Write"</table>"
connection.Close
Set connection = Nothing
%>
</body>
</html>
See the following edit in your code -
you create a recordset and then open it and execute it
<html>
<body>
<%
Dim sConnString, connection, sSQL, backgroundColor,objrs
sSQL = "SELECT firstName, lastName, email, zip, country, company, industry, revenue, timestamp FROM users"
Set connection = Server.CreateObject("ADODB.Connection")
connection.connectionstring = "put your connection string in here"
' replace put your connection string in here with your connection string
Set objrs= Server.CreateObject("ADODB.Recordset")
connection.Open()
objrs.open sSQL,connection
Response.Write"<table>"
do while not objrs.EOF
if(backgroundColor="#f1f1f1")then
backgroundColor="#ffffff"
else
backgroundColor="#f1f1f1"
end if
response.write"<tr backgroundColor="& backgroundColor & "></td>" & objrs("firstName") & "</td><td>" & objrs("lastName") & "</td><td>" & objrs("email") & "</td><td>" & objrs("zip") & "</td><td>" & objrs("country") & "</td><td>" & objrs("company") & "</td><td>" & objrs("industry") & "</td><td>" & objrs("revenue") & "</td><td>" & objrs("timestamp") & "</td></tr>"
objrs.MoveNext
Loop
Response.Write"</table>"
%>
</body>
</html>
a note:--
this needs to be something like - not sure what database you are using -
sConnString= "Provider=sqloledb;Data Source=ServerName;Initial Catalog=DatebaseName "
or if you have a dsn connection then simply
sConnString = "DSN=xxxxx;uid=xxx;pwd=xxx"
You will need to create a Recordset to read the data from your SQL query. You have used connection itself.
try below example.
HERE
The code below is how to show/split 2 rows in a table.
But how do I get it to show/split it to 4 rows and not only two ???
dim bCloseRow
bCloseRow = True
Do while not objTrucks.eof
If bCloseRow Then response.write "<tr>"
bCloseRow = Not bCloseRow
response.write "<td><input type=checkbox name=dno value=" & objTrucks("TRUCK_NO") & objTrucks("TRUCK_NO") & ">" & objTrucks("TRUCK_NO") & "</td>"
If bCloseRow Then response.write "</tr>"
objTrucks.movenext
loop
Thanks for any help.
If there are more rows then four in the recordset you have to make a row more. And if there is less then four you have to insert some empty td tags.
Hope this helps
const iCols=4
dim iCount:iCount=0
if not objTrucks.eof then
response.write "<table>"
Do while not objTrucks.eof
if iCount=0 then response.write "<tr>"
response.write "<td><input type=checkbox name=dno value=" & objTrucks("TRUCK_NO") & objTrucks("TRUCK_NO") & ">" & objTrucks("TRUCK_NO") & "</td>"
objTrucks.movenext
iCount = iCount + 1
if iCount=iCols then
response.write "</tr>"
iCount=0
end if
loop
if iCount>0 then
for i = iCount to iCols-1
response.write "<td></td>"
next
response.write "</tr>"
end if
response.write "</table>"
end if
I would do this using a counter instead of a Boolean flag:
Const numCols = 4
Dim counter
counter = 0
Do While Not objTrucks.EOF
If counter Mod numCols = 0 Then
Response.Write "<tr>"
End If
counter = counter + 1
Response.Write "<td><input type=checkbox name=dno value=" & objTrucks("TRUCK_NO") & objTrucks("TRUCK_NO") & ">" & objTrucks("TRUCK_NO") & "</td>"
If counter Mod numCols = 0 Then
Response.Write "</tr>"
End If
objTrucks.MoveNext
Loop
I have a classic ASP question.
Attempting to do this: the recordset is a simple list of years from 1995 to 2020; and i am trying to make 2010(current year) the default selection in the drop down.
issue: I trying to call a Sub proc in "Response.Write", but it keeps giving me this error:
"Error '800a000d' Type mismatch: 'selectyear' "
Below is the code, the Attempt 1 works with out any problem. But when i move that "if" logic to a sub procedure and call it in the Request.Write, it gives me the error.
Can any one please explain why Attempt1 works and Attempt2 wouldnt.
' Attempt 1:
rsYEAR.Open qYEAR, objconn, 0, 1
response.Write "<tr><td>Year:</td> <td> <select name='theyear' style=""WIDTH: 67px"">"
dim selyr
while not rsYEAR.EOF
if CINT(rsYEAR.fields("year")) = year(now) then
selyr = "selected"
else selyr = ""
end if
Response.Write"<option value='" & rsYEAR.fields("year") & "' "& selyr &" >" & cstr(rsYEAR.Fields("year"))
rsYEAR.MoveNext
wend
response.Write "</select></td></tr>"
rsYEAR.Close
' Attempt 2:
rsYEAR.Open qYEAR, objconn, 0, 1
response.Write "<tr><td>Year:</td> <td> <select name='theyear' style=""WIDTH: 67px"">"
dim selyr2
while not rsYEAR.EOF
Response.Write "<option value='" & rsYEAR.fields("year") & "' " & cstr(selectyear(cint(rsYEAR.fields("year")))) &" >" & cstr(rsYEAR.Fields("year"))
rsYEAR.MoveNext
wend
response.Write "</select></td></tr>"
'close and clean up
rsYEAR.Close
set rsYEAR = nothing
I would greatly appreciate your response.
thank you,
Shiva
I am guessing that cint(rsYEAR.fields("year")) is throwing the error because there is data that cannot be converted to int. I would expect that to happen in both cases though.
You shouldn't need the cstr in cstr(selectyear(cint(rsYEAR.fields("year")))) in the second attempt, as I assume selectyear is already returning a string. Can you show the code for selectyear?
(How has this sat for so long without a correct answer?)
A Sub does not have a value, so you can't Response.Write it. You need to either use a function, or put the Sub call on its own line.
rsYEAR.Open qYEAR, objconn, 0, 1
response.Write "<tr><td>Year:</td><td><select name='theyear' style=""width: 67px"">"
dim y
while not rsYEAR.EOF
y = rsYEAR.fields("year")
Response.Write "<option value='" & y & "'" & IsCurr(y) & ">" & y & "</option>"
rsYEAR.MoveNext
wend
response.Write "</select></td></tr>"
rsYEAR.Close
Function IsCurr(yr)
if Cstr(yr) = Cstr(year(now)) then
IsCurr = " selected"
else
IsCurr = ""
end if
End Function
Using a sub instead of a function, this would become
rsYEAR.Open qYEAR, objconn, 0, 1
response.Write "<tr><td>Year:</td><td><select name='theyear' style=""width: 67px"">"
dim y
while not rsYEAR.EOF
y = rsYEAR.fields("year")
Response.Write "<option value='" & y & "'"
IsCurr y
Response.Write ">" & y & "</option>"
rsYEAR.MoveNext
wend
response.Write "</select></td></tr>"
rsYEAR.Close
Sub IsCurr(yr)
if Cstr(yr) = Cstr(year(now)) then
Response.Write " selected"
end if
End Sub
Hi i have a problem with this asp pagination. it seems to be putting all the links in the one row, so i think it might have something to do with the check of the int i...
but im not that familar with asp. can anyone shed any light on this problem.
the folders contain pdfs for each day of the month, named A08P2.pdf A09P2.pdf etc...
Thanks
i = 1
Set fc = f.Files
Set ff = f.SubFolders
For Each f1 in fc
intPage = cint(mid(f1.name,2,2))
chrEdition = mid(f1.name,1,1)
if chrEdition = "A" then
if i = 1 then
Response.Write "<tr>"
end if
Response.Write "<td width='40' align='center'><a href=" & sUP & f1.name & " class='blue_11px'>" & intPage & "</a></td>"
if i = 10 then
Response.Write "</tr>"
i = 0
end if
end if
i = i + 1
Next
You should move the incrementing of the i (i=i+1) inside the if...end if, since if i is 9 and you encounter two chrEditions that are not 'A' then i will become 11 and will never match the closing condition i=10:
if chrEdition = "A" then
if i = 1 then
Response.Write "<tr>"
end if
Response.Write "<td width='40' align='center'><a href=" & sUP & f1.name & " class='blue_11px'>" & intPage & "</a></td>"
if i = 10 then
Response.Write "</tr>"
i = 0
end if
i = i + 1
end if