ASP - Printing the entire request contents - asp-classic

I'm debugging some ASP code and I need to get a quick printout of the current Request datastructure, which I believe is an array of key/value pairs.
I see that Request.Form("key") is the method for extracting individual elements.
Any tips on printing out the entire thing?

Try this
For Each item In Request.Form
Response.Write "Key: " & item & " - Value: " & Request.Form(item) & "<BR />"
Next

Working:
For x = 1 to Request.Form.Count
Response.Write x & ": " _
& Request.Form.Key(x) & "=" & Request.Form.Item(x) & "<BR>"
Next
I have other Classic ASP code snippets here:
https://github.com/RaviRamDhali/programming-procedure/tree/master/Snippets/ASP

Try a FOR/EACH loop:
for each x in Request.Form
Response.Write(x)
Next

Related

Exclude certain selections from a drop down list ASP

I'm working on a ASP (Within the payment gateway). The options in the drop down at being pulling in from a database. I can't touch the database so have to attack the matter from the code.
Here is the value name that I need to exclude _01BM(Q)
Here is the code for the drop down.
<select name="programgroup" onchange="onProgramGroup()">
<% Call buildDropDownList(strProgramGroupCode, rsProgramGroup, "ProgramGroupCode", "ProgramGroupDescription", False)%>
</select>
I would really appreciate any help on this guys.
Here is the code for the method:
Sub buildDropDownList(strCurrentSelection, objListData, strCodeName, strDescriptionName, blnIncludeOther)
If Not objListData.BOF Then
objListData.MoveFirst
End If
While Not objListData.EOF
Response.Write "<option value='" & objListData(strCodeName) & "' "
If StrComp(strCurrentSelection, objListData(strCodeName),1) = 0 then
Response.Write "selected"
End If
Response.Write ">" & objListData(strDescriptionName) & "</option>" & VbCrLf
objListData.MoveNext
Wend
if blnIncludeOther then
Response.Write "<option value='<Other>' "
If strCurrentSelection <> "" and InStr(1, "<Other>", strCurrentSelection) = 1 then
Response.Write "selected"
End If
Response.Write ">Other</option>" & VbCrLf
end if
End Sub
You will have to change the method building the drop down. Since you did not provide us with the code for it, I'll give you a skeleton, and can use it to change your actual code.
To make it more generic and less ugly, better pass the value(s) to exclude to the method, as an array, instead of hard coding them in there.
So, the method should look like this:
Sub buildDropDownList(strProgramGroupCode, rsProgramGroup, someParamHere, anotherParam, boolParam, arrValuesToExclude)
Dim excludedValuesMapping, x
Set excludedValuesMapping = Server.CreateObject("Scripting.Dictionary")
For x=0 To UBound(arrValuesToExclude)
excludedValuesMapping.Add(LCase(arrValuesToExclude(x)), True)
Next
'...unknown code here...
Do Until rsProgramGroup.EOF
strCurrentValue = rsProgramGroup(valueFieldName)
If excludedValuesMapping.Exists(LCase(strCurrentValue)) Then
'value should be excluded, you can do something here, but not write it to browser
Else
strCurrentText = rsProgramGroup(textFieldName)
Response.Write("<option value=""" & Replace(strCurrentValue, """", """) & """>" & strCurrentText & "</option>")
End If
rsProgramGroup.MoveNext
Loop
End Sub
And to use it:
<% Call buildDropDownList(strProgramGroupCode, rsProgramGroup, "ProgramGroupCode", "ProgramGroupDescription", False, Array("_01BM(Q)"))%>
Now having your code, and if you don't want to make it generic, you can also ignore specific values by having such code in the buildDropDownList sub:
Dim currentCodeValue
While Not objListData.EOF
currentCodeValue = objListData(strCodeName)
If (UCase(currentCodeValue)<>"_04GIDBM") And _
(UCase(currentCodeValue)<>"_05GIDFM") And _
(UCase(currentCodeValue)<>"_08EXHRM") And _
(UCase(currentCodeValue)<>"_10EXMKT") And _
(UCase(currentCodeValue)<>"_12EXTTH") And _
(UCase(currentCodeValue)<>"_17EXHSC") Then
Response.Write "<option value='" & currentCodeValue & "' "
If StrComp(strCurrentSelection, currentCodeValue, 1) = 0 then
Response.Write "selected"
End If
Response.Write ">" & objListData(strDescriptionName) & "</option>" & VbCrLf
End If
objListData.MoveNext
Wend
This will just skip any records with such values, and will not output a drop down option for them.

ASP Radio Button Null Value

I have an "edit" form that is pulling data from a specific row. Part of this form includes radio buttons (a set of four). I am able to retrieve the data from the radio button that has been selected but the other three don't have anything and I get a Null Value error. How could I prevent this? I essentially have 1 cell that pushes in the value of the radio button that was selected. In my asp code I have it set up like this:
<input <%If (CStr((rsCourseNum.Fields.Item("question1Correct").Value)) = CStr("answer1")) Then Response.Write("checked=""checked""") : Response.Write("")%> type="radio" name="question1Correct" id="question1Correct" value="answer1">
this would throw an error if answer0 was in the db since there is no answer1, I'm just not sure exactly how to set this up to prevent it from calling a null value.
What's the Response.Write("") for?
You're not getting an error because you're checking a db value that happens to be Null; you're getting an error because you're trying to convert a Null to a string. There are two* ways around this. Method one is to not do any data type conversions:
Response.Write "<input type=""radio"" name=""question1Correct"" id=""q1c"""
If rsCourseNum("question1Correct") = "answer1" Then Response.Write " checked"
Response.Write " value=""answer1""><label for=""q1c"">Question 1</label>"
This will work with Nulls because the comparison Null = "answer1" will return Null, which isn't True, so the button isn't marked.
The other method is to explicitly check for Nulls, and only do a data type conversion if the value isn't null.
Response.Write "<input type='radio' name='q" & i & "' id='q" & i & "c'"
If Not IsNull(rs("q" & i)) Then
If CStr(rs("q" & i)) = CStr(answers(i,0)) Then Response.Write " checked"
End If
Response.Write " value='" & answers(i,0) & "'>"
Response.Write "<label for='q" & i & "c'>" & answers(i,1) & "</label>"
* Well, two, uh, "proper" ways around this. There's also the hacky way: append a blank string instead of using CStr. (Thanks for the tip, Lankymart!)
If rs("q" & i) & "" = CStr(answers(i,0)) Then Response.Write " checked"

how to add my value into asp format

Dim adoRecordset
Remark is the value saved in SQL
my html value is
value="<%=adoRecordset("Remark")%>">
so how could i add it into the " ??? "
response.Write VALUE='???'
Thanks Guys ~
I think this should do the trick for you:
Response.Write "<value='" & adoRecordset("Remark") & "' name='test'>"
Edit: My quick and lazy version, as done properly by Mr. Shadow Wizard
Response.Write("<input name=""test"" value=""" & Replace(adoRecordset("Remark"), """", """) & """ />")

How to convert a string to XML & Get that XML ChildNode values in Classic asp?

`Here I have converted one string to XML:
xmlString =
" <?xml version='1.0' encoding='UTF-8' standalone='yes'?>" & _
" <hub:notifications>" & _
" <hub:notificationId>728dc361-8b4f-4acc-ad2d-9a63125c5114</hub:notificationId>" & _
" <hub:notificationId>5b7c6989-ee27-422c-bbed-2f2c36136c5b</hub:notificationId>" & _
" <hub:notificationId>67d1fffe-ab3f-43e3-bb03-24926debe2dc</hub:notificationId>" & _
" </hub:notifications>"
objXML.LoadXml(xmlString)
set Node = objXML.selectSingleNode("hub:notifications/hub:notificationId")
i = 0
Count = 0
For Each Node In objXML.selectNodes("hub:notifications")
ReDim Preserve aryNotificationIDs (i + 1)
aryNotificationIDs(i) = Node.selectSingleNode("hub:notificationId").text
Count++
Next
Response.write Count
In above, I am not getting Count of Child nodes
and How to get the child node values.
Can any one help me?
Thanks,
Jagadi`
There are many problems with your posted code.
FirstWhat language are you using? There seems to be styles from VBScript and JScript. It is predominately VBScript so I'm going to assume that is what you meant to use throughout.
Second
The XML declaration needs to be the first characters in the string.
That is:
"<?xml version='1.0' encoding='UTF-8' standalone='yes'?>" & _
not
" <?xml version='1.0' encoding='UTF-8' standalone='yes'?>" & _
Third
XML with namespaces requires an xml namespace declaration in the top-level node that use the namespace.
For example the root node.
<hub:notifications>
would become
<hub:notifications xmlns:hub='http://stackoverflow.com'>
But you would replace the stackoverflow URL with one appropriate to you.
Fourth
If you want to iterate through the child nodes of hub:notifications then you need to change the FOR deceleration to:
For Each Node In objXML.selectSingleNode("hub:notifications").childNodes
Fifth
i is not increasing in your loop, so you are setting aryNotificationIDs(1) to the different values of the nodes.
Sixth
Related to the first. There is no ++ operator in VBScript. And you don't need both i and Count in the For loop.
Additionally
You don't need to cycle through the nodes to get a count. You can use an xpath selector, and the length property. E.g. objXML.selectNodes("hub:notifications/hub:notificationId").length
Finally
I have taken you code and applied the above suggestions, I've also included an error checking part that checks that the xml has correctly loaded. The code below will output the count of hub:notificationId nodes, and list all the values in the array aryNotificationIDs. I've removed other superfluous code.
xmlString = "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>" & _
" <hub:notifications xmlns:hub='http://stackoverflow.com'>" & _
" <hub:notificationId>728dc361-8b4f-4acc-ad2d-9a63125c5114</hub:notificationId>" & _
" <hub:notificationId>5b7c6989-ee27-422c-bbed-2f2c36136c5b</hub:notificationId>" & _
" <hub:notificationId>67d1fffe-ab3f-43e3-bb03-24926debe2dc</hub:notificationId>" & _
" </hub:notifications>"
Set objXML = Server.CreateObject("Msxml2.DOMDocument")
objXML.LoadXml(xmlString)
If objXML.parseError.errorCode <> 0 Then
Response.Write "<p>Parse Error Reason: " & objXML.parseError.reason & "</p>"
Else
For Each node In objXML.selectSingleNode("hub:notifications").childNodes
ReDim Preserve aryNotificationIDs(i)
aryNotificationIDs(i) = node.text
i = i + 1
Next
Response.Write "<p>Count: " & i & "</p>"
For j = 0 to i - 1
Response.Write "<p>aryNotificationIDs(" & j & ") = " & aryNotificationIDs(j) & "</p>"
Next
End If

How to Validate a textbox+dropdowns in vb for a asp.net form

Previous question which links onto this and has any addition code ref should I forget to link any, I have set it up to email me should someone submit this form and an error occur and right now should that occur for most integer or datetime fields if they fail to validate then it will show me which fields in the email failed and what was input into them.
Problem I'm having now is to validate the drop downs and the textboxs in a similar way to what I with integer and datetime fields so I can display those also in the email in case they error.
present integer and datetime validation
Catch ex As Exception
lblInformation.Text = ("<h4>Unable to save data in database</h4>" + vbNewLine + "The error was '" + ex.Message + "'" + vbNewLine + vbNewLine + vbNewLine + "The SQL Command which falied was:" + vbNewLine + "<strong>" + mySQL + "</strong>" + vbNewLine).Replace(vbNewLine, "<br />" + vbNewLine)
Dim dtb As DateTime
If Not DateTime.TryParse(DateOfBirth, dtb) Then
strEMessageBody.Append("<strong>Date Of Birth:</strong> " & DateOfBirthYear.SelectedItem.Value & "-" & DateOfBirthMonth.SelectedItem.Value & "-" & DateOfBirthDay.SelectedItem.Value & vbCrLf)
strEMessageBody.Append("<br/>" & vbTab & vbTab & vbTab & vbTab & vbTab & vbTab)
End If
Dim iao As Integer
If Not Integer.TryParse(AnyOther, iao) Then
strEMessageBody.Append("<strong>Any Other:</strong> " & rblAnyOther.Text & vbCrLf)
strEMessageBody.Append("<br/>" & vbTab & vbTab & vbTab & vbTab & vbTab & vbTab)
End If
then below the final validation I have the Dim for the email setting but that I sorted out in the other question.
The problem is much earlier in the page I have
Sub Upload_Click(ByVal source As Object, ByVal e As EventArgs)
If (Page.IsValid) Then
Dim Name As String
Which prevents me just using there names as shown above where I would instead call them something else but that doesn't work with strings so my main issue is having some bit of code to check if the strings are valid and for the dropdowns which would either work but always show the data in the email or would hiccup in the code,
Dim imd As Integer
If Not Integer.TryParse(dept, imd) Then
strEMessageBody.Append("<strong>Department:</strong> " & dept.Text & vbCrLf)
strEMessageBody.Append("<br/>" & vbTab & vbTab & vbTab & vbTab & vbTab & vbTab)
End If
below was how it had been setup to record the department
Department = dept.SelectedItem.Value
Department = Replace(Department, "'", "''")
Summary:- Need vb code to validate if strings and dropdowns are valid and the use of try/catch block is another possible solution but I wasn't able to figure out how to implement validation for that either.
Log your values into your database. Setup a logging table called "tblLog" or something else. Record the value of ex.Message or possibly even InnerException (if it exists).
Going hand in hand with Matt's answer, there is a tool that can help you with automatically logging errors to a DB.
It's called ELMAH.
EDIT
Here are 2 validations that you might want to use:
Dim s As String = "some user input in here"
If [String].IsNullOrEmpty(s) Then
' Watch out, string is null or it is an empty string
End If
Dim cb As New ComboBox()
If cb.SelectedItem Is Nothing Then
' Watch out, combo has no item selected
End If
NOTE ComboBox is a WinForm control in this example, but the idea is the same for the ASP.NET counterpart
Since everybodies given up trying to find a solution then I'm just gona close this topic with this post as the answer.

Resources