Solved

VBScript String Variable Is Acting Like and Reporting Null

Posted on 2013-01-24
4
513 Views
Last Modified: 2013-01-30
I need help figuring out an issue w/ a variable. I'll call it Snippet

I'm using a scripting dictionary to pass values around an ASP page, and an item in it should contain a string value (Snippet); and by all appearances it does. When I grab it out of the database I can response.write recordset("Snippet").value
When I tried running it through a battery of replace functions, it failed w/ the following error, and testing it w/ IsNull() returns true, as well:
"
vbscript Microsoft VBScript runtime error '800a005e'  
Invalid use of Null: 'Replace'
"

Here's an example of almost literally what the variable holds. The only change I've made, copying it straight out of the database via an SQL client, is to run a regular expression using EditPad Pro to replace most "words" with "x" before providing it as a sample here:

																				<br />
The x x x x x x x x x x x. x x x x x x x x x x x x. x x x x x x x x x x x x x. x x x x x x x x if

Open in new window


It contains tabs and spaces and one carriage return. I'm viewing it and its "hidden characters" in EditPad Pro. All instances of Snippet are failing as Null, not just this one.

The value is created from a larger string drawn from users' HTML.
This is all that happens to create it:
objDataDict.Add "Snippet", DecodeSpecChars(objDataDict.Item("HTMLContent"))
' Collect the portion of HTMLContent up to the pagebreak indicator...
arrTmp = Split(objDataDict.Item("Snippet"), "[!]")
objDataDict.Item("Snippet") = arrTmp(0)
arrTmp = Empty

if ( Not IsNull(objDataDict.Item("Snippet")) AND objDataDict.Item("Snippet") <> "" ) then
	' Prepare and cleanup HTML in content for display in context...
	' If there are any "img" tags in Snippet, strip them out...
	' Prep regular expression to find images...
	Set objRegExp = New RegExp
	objRegExp.IgnoreCase = TRUE
	objRegExp.Global = TRUE
	strRE = "[<]a[^>]+>[<]img[^>]+>[<][^>]+>"   ' RegExp to remove image tags surrounded by links
	objRegExp.Pattern = strRE
	objDataDict.Item("Snippet") = objRegExp.Replace(objDataDict.Item("Snippet"), "")
	strRE = "[<]img[^>]+>"   ' RegExp for any remaining image tags
	objRegExp.Pattern = strRE
	objDataDict.Item("Snippet") = objRegExp.Replace(objDataDict.Item("Snippet"), "")
	strRE = "[<]br[^>]*>\B"   ' RegExp to remove line break tags butting up against text
	objRegExp.Pattern = strRE
	objDataDict.Item("Snippet") = objRegExp.Replace(objDataDict.Item("Snippet"), "")
	strRE = "[<]p[^>]*>"   ' RegExp to remove opening paragraph tags
	objRegExp.Pattern = strRE
	objDataDict.Item("Snippet") = objRegExp.Replace(objDataDict.Item("Snippet"), "")
	strRE = "[<]/p>"   ' RegExp to replace closing paragraph tags w/ line breaks
	objRegExp.Pattern = strRE
	objDataDict.Item("Snippet") = objRegExp.Replace(objDataDict.Item("Snippet"), "<br />")
	
	objDataDict.Item("Snippet") = Replace(objDataDict.Item("Snippet"), "&nbsp;", Chr(32))
	objDataDict.Item("Snippet") = Trim(EncodeSpecChars(objDataDict.Item("Snippet")))
else
	objDataDict.Item("Snippet") = ""
end if

Open in new window


I even test Snippet for Null to begin with.
This and a pair of other custom functions are all that touch it between instantiation, writing to the database, fetching from the database, assigning it to the dictionary again, and writing it back out to the page.

I have the other functions pared down to this, at this point (while I sort out issues w/ character encoding):
' Replace quotes that cause problems, and HTML encode other special characters as well
Function EncodeSpecChars(ByRef Data)
	Data = Replace(Data,Chr(34),"&quot;") ' "
	Data = Replace(Data,Chr(39),"&Xapos;") ' '
	Data = Replace(Data,"'","&Xapos;") ' '
	
	EncodeSpecChars = Data
End Function

' Reinsert original characters, except ", because they mess up text inputs
Function DecodeSpecChars(ByRef Data)
	Data = Replace(Data,"&Xapos;",Chr(39))
	
	DecodeSpecChars = Data
End Function

Open in new window


 Any chance issues w/ character encoding could be related?

This is driving me nuts, because I'm almost finished w/ this dang project, and there just has to be this one "last" problem. Thank you tremendously for any help!
0
Comment
Question by:universalglove
  • 3
4 Comments
 

Author Comment

by:universalglove
Comment Utility
Here's an example of what I'm doing to test it and prep it on the final page, where it's being put to use:

do until ( objRS.eof )
	Response.Write "ID: " & objRS("ID").value & "<br />" & vbCrLf
	if IsNull(objRS("ID").value) then
		Response.Write "Found to be null." & "<br />" & vbCrLf
	else
		objDataDict.Add "ID", objRS("ID").value
	end if
	Response.Write "Snippet: " & objRS("Snippet").value & "<br />" & vbCrLf
	if IsNull(objRS("Snippet").value) then
		Response.Write "Found to be null." & "<br />" & vbCrLf
	else
		objDataDict.Add "Snippet", DecodeSpecChars(objRS("Snippet").value)
	end if
	
	' ...
	
	Response.Write "stuff and things and " & objDataDict.Item("Snippet") & "<br /><br />" & vbCrLf

	objDataDict.RemoveAll
	objRS.MoveNext

loop

Open in new window

0
 
LVL 52

Assisted Solution

by:Scott Fell, EE MVE
Scott Fell,  EE MVE earned 500 total points
Comment Utility
Please look at the original code you are using, which specific line are you getting the error.

Is it line 16 above ? objDataDict.Item("Snippet") = objRegExp.Replace(objDataDict.Item("Snippet"), "")

Can you test these before this line?  what is response.write objDataDict.Item("Snippet")  and on a separate line  response.write objRegExp.Replace(objDataDict.Item("Snippet"), "")

It's hard to tell because you are hiding your code.
If ou are adding multiple items to your dictionary, it would be

objDataDict.Add "Snippet1", DecodeSpecChars(objDataDict.Item("HTMLContent1"))
objDataDict.Add "Snippet2", DecodeSpecChars(objDataDict.Item("HTMLContent2"))
objDataDict.Add "Snippet3", DecodeSpecChars(objDataDict.Item("HTMLContent3"))

response.write "Snippet2 = "&response.write (objDataDict.item("snipet2")
0
 

Accepted Solution

by:
universalglove earned 0 total points
Comment Utility
Well... what the heck!?
The world has rearranged itself since I last did a thorough testing, and now the code is suddenly working!

While prepping the code to submit for review, I rewrote and reorganized what I was using to test, and now it's all working.
• I did a test without doing anything to objRS("Snippet") before assigning it to the variable, and then tested and ran DecodeSpecChars() on it and retested. Works great.
• Rearranged the test to run DecodeSpecChars() on objRS("Snippet") before assigning it to the variable - no problem.
• I thought it may be the difference between ByRef and ByVal in DecodeSpecChars(), which I was going to test this morning but apparently had already changed previously. But switching ByVal/ByRef in the function now changes nothing.
• If I response.write objRS("Snippet") before assigning it to objDataDict, I run into a problem. Makes no sense to me why I can do that with other columns in the recordset, and I wasn't doing that when I initially ran into a problem with DecodeSpecChars() reporting it was being given a null value.

I must have changed something else along the way that remedied the problem.
• Maybe another column gave me problems initially, and I generated problems for myself during testing, and I've since remedied the other columns.
• Maybe the processing of the other special characters in DecodeSpecChars() was generating a problem. Not going to bother with it for now, due to my issues straightening out character encoding.
• Maybe I'm just too scatterbrained to be a programmer when I feel pressured...

Well, for posterity, here's the chunks of code I was going to submit
(I went through all the trouble of prepping them, I might as well use them, and then I can see how this code snippet embed works, too)...

Chunk of script that writes to the page:
<%
SQLS = "SELECT Database.ListingID, Database.ListingURL, Database.ListingTitle, Database.ListingDate, Database.ListingSnippet, Database.ListingImgURL, Database.ListingApproved, Database.ForceDisplay, Database.DisplayDate, Database.ListingLinkText, UserDatabase.MemberNumber, UserDatabase.MemberFirstName, UserDatabase.MemberLastName " & _
"FROM Database INNER JOIN BlogDatabase ON Database.BlogID = BlogDatabase.BlogID INNER JOIN UserDatabase ON BlogDatabase.MemberNumber = UserDatabase.MemberNumber " & _
"WHERE BlogDatabase.Display <> 'false' AND Database.ListingApproved = 'true' " & _
"ORDER BY Database.ListingDate DESC"

Set objRS = ExecuteQuery(SQLS,Nothing,FALSE)

do until ( objRS.eof )
Response.Write "ListingID: " & objRS("ListingID").value & "<br />" & vbCrLf
if IsNull(objRS("ListingID").value) then
	Response.Write "Found to be null." & "<br />" & vbCrLf
else
	objDataDict.Add "ListingID", objRS("ListingID").value
end if
Response.Write "ListingURL: " & objRS("ListingURL").value & "<br />" & vbCrLf
if IsNull(objRS("ListingURL").value) then
	Response.Write "Found to be null." & "<br />" & vbCrLf
else
	objDataDict.Add "ListingURL", DecodeSpecChars(objRS("ListingURL").value)
end if
Response.Write "ListingTitle: " & objRS("ListingTitle").value & "<br />" & vbCrLf
if IsNull(objRS("ListingTitle").value) then
	Response.Write "Found to be null." & "<br />" & vbCrLf
else
	objDataDict.Add "ListingTitle", DecodeSpecChars(objRS("ListingTitle").value)
end if
Response.Write "ListingDate: " & objRS("ListingDate").value & "<br />" & vbCrLf
if IsNull(objRS("ListingDate").value) then
	Response.Write "Found to be null." & "<br />" & vbCrLf
else
	objDataDict.Add "ListingDate", DecodeSpecChars(objRS("ListingDate").value)
end if

'''''''''''''''''''''''''''''''''
' Response.Write "ListingSnippet: " & objRS("ListingSnippet").value & "<br />" & vbCrLf
objDataDict.Add "ListingSnippet", DecodeSpecChars(objRS("ListingSnippet").value)
'dim tmp
'tmp = objRS("ListingSnippet").value
'Response.Write "tmp: " & tmp & "<br />" & vbCrLf
'if IsNull(tmp) then
'	Response.Write "tmp found to be null." & "<br />" & vbCrLf
'end if
'objDataDict.Item("ListingSnippet") = tmp
Response.Write "ListingSnippet: " & objDataDict.Item("ListingSnippet") & "<br />" & vbCrLf
if IsNull(objDataDict.Item("ListingSnippet")) then
	Response.Write "ListingSnippet found to be null." & "<br />" & vbCrLf
'else
'	objDataDict.Add "ListingSnippet", DecodeSpecChars(objRS("ListingSnippet").value)
end if
'objDataDict.Item("ListingSnippet") = DecodeSpecChars(objDataDict.Item("ListingSnippet"))
'Response.Write "ListingSnippet(post proc): " & objDataDict.Item("ListingSnippet") & "<br />" & vbCrLf
'if IsNull(objDataDict.Item("ListingSnippet")) then
'	Response.Write "ListingSnippet found to be null after DecodeSpecChars()." & "<br />" & vbCrLf
'end if
'''''''''''''''''''''''''''''''''

Response.Write "ListingImgURL: " & objRS("ListingImgURL").value & "<br />" & vbCrLf
if IsNull(objRS("ListingImgURL").value) then
	Response.Write "Found to be null." & "<br />" & vbCrLf
else
	objDataDict.Add "ListingImgURL", DecodeSpecChars(objRS("ListingImgURL").value)
end if
Response.Write "ListingApproved: " & objRS("ListingApproved").value & "<br />" & vbCrLf
if IsNull(objRS("ListingApproved").value) then
	Response.Write "Found to be null." & "<br />" & vbCrLf
else
	objDataDict.Add "ListingApproved", objRS("ListingApproved").value
end if
Response.Write "ForceDisplay: " & objRS("ForceDisplay").value & "<br />" & vbCrLf
if IsNull(objRS("ForceDisplay").value) then
	Response.Write "Found to be null." & "<br />" & vbCrLf
else
	objDataDict.Add "ForceDisplay", objRS("ForceDisplay").value
end if
Response.Write "DisplayDate: " & objRS("DisplayDate").value & "<br />" & vbCrLf
if IsNull(objRS("DisplayDate").value) then
	Response.Write "Found to be null." & "<br />" & vbCrLf
else
	objDataDict.Add "DisplayDate", DecodeSpecChars(objRS("DisplayDate").value)
end if
Response.Write "ListingLinkText: " & objRS("ListingLinkText").value & "<br />" & vbCrLf
if IsNull(objRS("ListingLinkText").value) then
	Response.Write "Found to be null." & "<br />" & vbCrLf
else
	objDataDict.Add "ListingLinkText", DecodeSpecChars(objRS("ListingLinkText").value)
end if
Response.Write "MemberNumber: " & objRS("MemberNumber").value & "<br />" & vbCrLf
if IsNull(objRS("MemberNumber").value) then
	Response.Write "Found to be null." & "<br />" & vbCrLf
else
	objDataDict.Add "MemberNumber", objRS("MemberNumber").value
end if
Response.Write "MemberFirstName: " & objRS("MemberFirstName").value & "<br />" & vbCrLf
if IsNull(objRS("MemberFirstName").value) then
	Response.Write "Found to be null." & "<br />" & vbCrLf
else
	objDataDict.Add "MemberFirstName", DecodeSpecChars(objRS("MemberFirstName").value)
end if
Response.Write "MemberLastName: " & objRS("MemberLastName").value & "<br />" & vbCrLf
if IsNull(objRS("MemberLastName").value) then
	Response.Write "Found to be null." & "<br />" & vbCrLf
else
	objDataDict.Add "MemberLastName", DecodeSpecChars(objRS("MemberLastName").value)
end if

'''
Response.Write "<br />Processing #" & objDataDict.Item("ListingID") & "<br /><br />" & vbCrLf

' Some further display determinations...
' And insertion of objDataDict's data into the HTML template...

'''
Response.Write "<br />Finishing processing of #" & objDataDict.Item("ListingID") & "<br /><br />" & vbCrLf

	objDataDict.RemoveAll
	objRS.MoveNext
loop

Call ConnectionCleanup(objRS)
%>

Open in new window


The more complete version of the 1st process containing the RegExp I provided above:
' Submitting a form...
Sub Process_Submission()
	dim objFormData
	
	' Prep data submitted through form. Using Persits Upload module, which also grabs all form data.
	Set objFormData = Server.CreateObject("Persits.Upload.1")
	objFormData.Save(Request.ServerVariables("APPL_PHYSICAL_PATH") & "Management\temp\")
	' The hidden input "FormAction" contains the command for determining the path of execution.
	semAction = objFormData.Form("FormAction")
	strFormErrors = ""
	
	if ( IsNull(semAction) OR "" = semAction ) then		
		semAction = "reload"
	end if

	' Determine required path of execution...
	select case semAction
		' In case the button to "reload" was clicked. Should be getting handled by Javascript.
		case "reload"
			Call Cleanup_Data(objFormData)
			Response.Redirect(strThisPagesName)
			Exit Sub
		' In case the button to "fetch feeds" was clicked. Should be getting handled by Javascript.
		case "fetch"
			Call Cleanup_Data(objFormData)
			Response.Redirect(strThisPagesName & "?action=fetch")
			Exit Sub
		case "goto"
			Call Cleanup_Data(objFormData)
			Response.Redirect("http://www.example.com/Management/user-mgmt.asp")
			Exit Sub
		case "delete"
			objDataDict.Add "ListingID", objFormData.Form("ListingID")
		case "insert"
			' Set form data to objDataDict global dictionary object...
			for each strKey in objFormData.Form
				objDataDict.Add strKey.Name, strKey.Value
			next

			' Validate and then process...
			Call Validate_Data()
		case "update"
			' Set form data to objDataDict global dictionary object...
			for each strKey in objFormData.Form
				objDataDict.Add strKey.Name, strKey.Value
			next

			' Validate and then process...
			Call Validate_Data()
		case else
			strFormErrors = strFormErrors & "<p class=""SubmitFailure"">The command for processing the submission was not properly set.<br /><span class=""SubmitFailureSubclause"">Please contact the webmaster at <a href=""mailto:webmaster@example.com"">webmaster@example.com</a> to chew him/her out for this error. Sorry for the inconvenience.</span></p>" & vbCrLf
				
			semAction="reload"
	end select
	' END Form Processing.
	' Corrections required? Return to form to try re-editing and saving the entry again...
	if semAction = "reload" then
		Call Return_To_Page(objFormData)
	' Otherwise, proceed updating DB...
	else
		Call Process_Data(objFormData)
	end if

End Sub
' END Process_Submission()


' Validate and prepare submitted data for processing...
Sub Validate_Data()
	dim arrTmp
	dim strRE
	dim objRegExp
	dim intErrCnt
	intErrCnt = 0

	' Processing of other variables and such...	
	for each strKey in objDataDict
		' Check that the following fields are neither empty nor null...
		' if ... blah, blah, blah end if
		' Check that the following fields are not null...
		if (strKey = "ListingImgURL" OR strKey = "ListingContent" OR strKey = "ListingLinkText") then
			if ( IsNull(objDataDict.Item(strKey)) ) then
				objDataDict.Item(strKey) = ""
			end if
		end if
		' set the checkbox value to 0 if it has no value (checkbox unchecked upon submission)...
		' if ... blah, blah, blah end if
	next

	' More processing...
	
	' Prep values for storage in database or writing to HTML
	for each strKey in objDataDict
		objDataDict.Item(strKey) = EncodeSpecChars(objDataDict.Item(strKey))
	next
	
	' Finalize any error messages and denote revisions required (All assignments to strFormErrors have been excised from the above.)
	if strFormErrors <> "" then 
		' Prep error message, blah blah blah ...
		semAction="reload"
		Exit Sub
	end if

'! Picking up where you've already seen:
	' Prepare snippet from ListingContent...
	objDataDict.Add "Snippet", DecodeSpecChars(objDataDict.Item("ListingContent"))
	' Collect the portion of ListingContent up to the pagebreak indicator...
	arrTmp = Split(objDataDict.Item("Snippet"), "[!]")
	objDataDict.Item("Snippet") = arrTmp(0)
	arrTmp = Empty
	
	if ( Not IsNull(objDataDict.Item("Snippet")) AND objDataDict.Item("Snippet") <> "" ) then
		' Prepare and cleanup HTML in content for display in context...
		' If there are any "img" tags in Snippet, strip them out...
		' Prep regular expression to find images...
		Set objRegExp = New RegExp
		objRegExp.IgnoreCase = TRUE
		objRegExp.Global = TRUE
		strRE = "[<]a[^>]+>[<]img[^>]+>[<][^>]+>"   ' RegExp to remove image tags surrounded by links
		objRegExp.Pattern = strRE
		objDataDict.Item("Snippet") = objRegExp.Replace(objDataDict.Item("Snippet"), "")
		strRE = "[<]img[^>]+>"   ' RegExp for any remaining image tags
		objRegExp.Pattern = strRE
		objDataDict.Item("Snippet") = objRegExp.Replace(objDataDict.Item("Snippet"), "")
'		strRE = "[<]p[^>]*>"   ' RegExp to remove opening paragraph tags
'		objRegExp.Pattern = strRE
'		objDataDict.Item("Snippet") = objRegExp.Replace(objDataDict.Item("Snippet"), "")
'		strRE = "[<]/p>"   ' RegExp to replace closing paragraph tags w/ line breaks
'		objRegExp.Pattern = strRE
'		objDataDict.Item("Snippet") = objRegExp.Replace(objDataDict.Item("Snippet"), "<br /><br />")
		
		objDataDict.Item("Snippet") = Replace(objDataDict.Item("Snippet"), "&nbsp;", " ")
		objDataDict.Item("Snippet") = Trim(EncodeSpecChars(objDataDict.Item("Snippet")))
	else
		objDataDict.Item("Snippet") = ""
	end if

End Sub
' END Validate_Data()


' Routine to process SQL statements, send to SQL Server, and proceed to confirmation
Sub Process_Data(UploadedData)
	dim objRS

	select case semAction
		case "delete"
			SQLS = "DELETE FROM Database " & _
			"WHERE ListingID = '" & objDataDict.Item("ListingID") & "'"

			Call ExecuteNonQuery(SQLS,Nothing,FALSE)
			semAction = "deleted"
		case "update"
			' Write out SQL to update submitted listing...
			SQLS = "UPDATE Database " & vbCrLf & _
			"SET ListingApproved = '" & objDataDict.Item("ListingApproved") & "', " & _
			"ForceDisplay = '" & objDataDict.Item("ForceDisplay") & "', "
			' Determine and set DisplayDate if ForceDisplay = TRUE...
			if ( 1 = objDataDict.Item("ForceDisplay") ) then
				dim dateToday, dateToDisplay
				dateToday = Date
				dateToDisplay = Year(dateToday) & "-" & Month(dateToday) & "-" & Day(dateToday) & " 00:01:02.003" 
				SQLS = SQLS & "DisplayDate = '" & dateToDisplay & "', "
			end if
			SQLS = SQLS & "ListingImgURL = '" & objDataDict.Item("ListingImgURL") & "', " & _
			"ListingTitle = '" & objDataDict.Item("ListingTitle") & "', " & _
			"ListingContent = '" & objDataDict.Item("ListingContent") & "', " & _
			"ListingSnippet = '" & objDataDict.Item("ListingSnippet") & "', " & _
			"ListingLinkText = '" & objDataDict.Item("ListingLinkText") & "' " & vbCrLf & _
			"WHERE ListingID = " & objDataDict.Item("ListingID")

			Call ExecuteNonQuery(SQLS,Nothing,FALSE)
			semAction = "success"
		case "insert"
			' Write out SQL to update submitted listing...
			SQLS = "INSERT INTO Database " & _
			"(BlogID, ListingURL, ListingTitle, ListingDate, ListingImgURL, ListingContent, ListingSnippet, ListingLinkText, ListingApproved, ForceDisplay, DisplayDate) " & vbCrLf & _
			"VALUES ('" & objDataDict.Item("BlogID") & "', " & _
			"'" & objDataDict.Item("ListingURL") & "', " & _
			"'" & objDataDict.Item("ListingTitle") & "', " & _
			"'" & objDataDict.Item("ListingDate") & "', " & _
			"'" & objDataDict.Item("ListingImgURL") & "', " & _
			"'" & objDataDict.Item("ListingContent") & "', " & _
			"'" & objDataDict.Item("ListingSnippet") & "', " & _
			"'" & objDataDict.Item("ListingLinkText") & "', " & _
			"'" & objDataDict.Item("ListingApproved") & "', " & _
			"'" & objDataDict.Item("ForceDisplay") & "', " & _
			"'" & objDataDict.Item("ListingDate") & "') "

			Call ExecuteNonQuery(SQLS,Nothing,FALSE)
			semAction = "success"
	end select

	' Free up resources, if objRS is set
	'	Call ConnectionCleanup(objRS)
	Call Return_To_Page(UploadedData)
End Sub
' END Process_Data()

Open in new window


Gaaaahhhh...
Well, thanks for tuning into another episode of my saga as a harebrained novice programmer.
0
 

Author Closing Comment

by:universalglove
Comment Utility
Thank you, padas, for reading through my post and inducing me to redo my testing. I'll award the points to you, since you did that much. I appreciate any attempts to help very much.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now