Changing ASP.Net label's text with VB.Net - asp.net

I currently have a form that has a label appear when there is an error with changing your password.
Currently there is a bug that makes a message pop up even when the change is successful that says, "Object reference not set to an instance of an object."
I'm wanting to write a test that says when the label is equal to that message to make the visibility false. I've tried:
If lblMsg.Text = "Object reference not set to an instance of an object." Then
lblMsg.Visible = False
End If
This, among other variations, have yet to work successfully for me.
Any idea on what other ways I can write this out to hide the label when that message occurs?

Try the following (not the text cannot be null, or this would be a problem):
If lblMsg.Text.ToLower().Trim() = "object reference not set to an instance of an object." Then
lblMsg.Visible = False
End If
If that doesn't work, then there is a character off in the message. Alternatively, why not figure out where the object reference error is occurring and fix that problem?

I figured out my error. I ended up having to write an IF statement that checked the response for nulls, i.e:
If response Is Nothing OrElse response.length = 0 then
returnString.AppendLine("Password Changed Successfully.")
End If
Thanks to any and all who commented and helped!

Related

Clear Required Field Validation Group

I have 14 Fields in my Web page; and for all of them I use RequiredFieldValidator.
This Validator it works fine for all the fields. And when I have an error it caches it and display it to the proper error message area.
In this area I have a button in order to clear the messages and return to the input page.
After I'm caching an error or two I return to the first page by pressing the reset button... correcting the errors... and go forth.
When I'm passing variables to all the fields (which means everything is good) then normally the Validator has to be change and pass the trap (which I have in my code) with no error messages in it...
Here is the trap code...
If Not String.IsNullOrEmpty(RegisterValidationSummary.ValidationGroup) Then
RegisterValidationSummary.Visible = True
ErrorPanel.Visible = True
btnClr.Visible = True
Return
End If
But no the trap works again and return me to the error area without any error displaying.
Only the reset button is there, and the error list is empty.
In order to be sure that all the errors are cleared I use in the reset button the following
Private Sub ClearValidators(sender As Object, e As EventArgs)
RegisterValidationSummary.ValidationGroup = String.Empty
End Sub
At this point the validators are cleared and the trap is not working... The problem is... that happens even when I have certain errors in my page or not.
Thus I use the following code in order to enable the Validators again.
For t = 0 To RegisterValidationSummary.Page.Validators.Count - 1
RegisterValidationSummary.Page.Validators.Item(t).Validate()
Next
Validators are not enabled and of course the trap is not working.
Is someone how knows what is going on and how I may have a certain error erase from validators... and enabled again in order to validate again the fields?
You are doing the validation wrong. First of all ValidationGroup is used to group validations so in a button click you validate the fields in that group. All server controls have this property so that you could group them together for validations by using the same text in this property. Checking the value in this property will not indicate that your values in your controls are valid or not.
So the following condition is always True
If Not String.IsNullOrEmpty(RegisterValidationSummary.ValidationGroup) Then
Therefore, you'll see the reset button all the time.
And, by doing the following you are removing the RegisterValidationSummary control from the ValidationGroup.
RegisterValidationSummary.ValidationGroup = String.Empty
Therefore, after this point you'll not see any error messages since there's no ValidationSummary in the validation group to show the summary of error messages.
Hope I explained why you are seeying what you are seeing right now. Ok, since we know the issue we can easily fix it now.
You should do something like this. Basically we should make use of Page.Validate() and If (Page.IsValid) Then condition to check whether all fields in the ValidationGroup are valid.
Page.Validate()
If (Page.IsValid) Then
' If this condition is true that means all your fields have correct values
Else
' If this condition is true some of your fields are invalid. You can see that in the Validation summary message.
' Not sure why you do the reset but your reset logic could go here
RegisterValidationSummary.Visible = True
ErrorPanel.Visible = True
btnClr.Visible = True
End If
Hope this helps to resolve your issue.

OnBase - Find Keyword with VBScript

I've been struggling with trying to find a Keyword in OnBase with VBScript. I'm not sure of the method to find and get the value based on the Keyword
This is what I have so far
'Keywords to Get
Const KEYWORD_VENDORNO = "Vendor Number"
Sub Main
'Create Application Object
Dim objApplication
Set objApplication = CreateObject("OnBase.Application")
'Get the current document
Dim objCurrentDocument
Set objCurrentDocument = objApplication.CurrentDocument
'Get Collection of Keywords
Dim colKeywords
Set colKeywords = objCurrentDocument.Keywords
'Set Keyword in memory
colKeywords.AddKeyword(KEYWORD_VENDORNO,"123")
'Save Keyword
objCurrentDocument.StoreKeywords
'Get Keyword - This is the part I don't know how to get. I can't seem to find the 'right property or method to get the value of the keyword
'I've tried all the following, but none seem to work. The only one that doesn't give me an 'error is the 2nd one, but it also doesn't give me a value
msgbox colKeywords(0)
msgbox colKeywords(KEYWORD_VENDORNO)
msgbox colKeywords.GetKeyword(KEYWORD_VENDORNO)
msgbox colKeywords.FindKeyword(KEYWORD_VENDORNO)
msgbox objCurrentDocument.GetKeyword(KEYWORD_VENDORNO)
msgbox objCurrentDocument.FindKeyword(KEYWORD_VENDORNO)
End Sub
Have you searched the OnBase Community for help? There is a lot of great resources there.
I did a quick search for "VB Keyword" and found this post asking about a document handle in VB Script. The answer was:
Try removing the "set" from before "documentHandle = document.Handle".
This is supported by this post that has examples WITHOUT the SET prefix.
Try dropping the SET prefix.

How to tell whether a variable in ASP has been declared

Let me start off by saying I'm a PHP developer, not an ASP one. (And I really wish ASP had isset().) And I'm working in a live environment so I don't really have an opportunity to do any testing.
All the resources I've found suggest different ways to test for the existence of a variable.
Here's what I'm trying to do:
On SOME pages, I set a variable which holds a value for a robots <meta> tag:
dim dsep_robots
dsep_robots = "nofollow,noindex"
All pages include header.asp. In my header file, I want to test whether dsep_robots has a value and if so, output that value, otherwise, output nothing.
I think that testing whether dsep_robots has a value might look like this:
if not dsep_robots = "" then
'...
end if
Best practices in PHP state that when you're using a variable that may or may not exist, you should always test if (isset($var)) {...} (not doing so will trigger a Notice if the variable doesn't exist).
Is there such a thing in ASP -- i.e. do I really need to test if it exists, or can I simply test if it has a value?
ust by the way, your question isn't about classic ASP, it is a VBScript question. VBScript can occur in scripts outside of ASP. And compilation isn't done in VBScript, because it is an interpreted language. Nevermind.
I think there's some confusion here -- and your question seems to have more to do with uninitialized variables than undeclared variables. For undeclared variables, see below.
For uninitialized, try the function IsEmpty.
For checking for null, try the function IsNull.
dim x
x = 1
dim t
Response.write isempty(x)
Response.write "<br>"
Response.write isempty(t)
Will display:
False
True
Detecting Undeclared Variables
If you include Option Explicit in your header, use of a non-declared variable will cause an runtime error. If your script is not Option Explicit it will not generate an error, and there is no function that will tell you if the variable has been declared or not. This sounds sloppy, and it is, but it was intentional.
The only way you can escape this is to actually set Option Explicit and then trap the error that you will get when you try to use the undeclared variable. If you trap this particular error you will find that it has Err.Number = 500. So, the following will do what you want:
Option Explicit
dim x
On Error Resume Next
Response.Write dsep_robots
If Err.Number > 0 Then
Response.Write Err.Number
end if
Of course, if you set Option Explicit and your code is rife with undeclared variables then you'll get errors being thrown all over the place, so you'd need to set On Error Resume Next at the top of your code so you can successfully ignore it, and only trap it when you want to.
By the way, here's Microsoft's online reference for VBScript:
http://msdn.microsoft.com/en-us/library/d1wf56tt(v=VS.85).aspx
#Jazzerus: I'd recommend putting the code within header.asp into a Sub, something like
Sub outputHeader(ByRef MyTitle, Byref dsep_robots)
'contents of header.asp
End Sub
...and then in your calling pages include header.asp right at the top and use
outputHeader "Title for this page", "value you want dsep_robots to have for page"
If you don't set dsep_robots on that page, then just leave the second parameter blank ("")
Then just checking if the variable is empty or not within the Sub should suffice:
If dsep_robots <> "" Then
Response.Write dsep_robots
End If
What about:
If NOT IsEmpty(myvariable) Then...
that seems to have been working for me.
I use the VarType function to detect if the variable is defined. If the tested variable is not defined, VarType returns the value vbError (10). Interesting to note that the optional parameter cannot be the last parameter else ASP tosses an error.
function Sample(p1,p2,p3,p4)
if VarType(p3) <> vbError then
'do something with p3
end if
end function
ThisWorks=Sample(1,2,,3)
ThisFails=Sample(1,2,3,)

comparing http_referer against http_host

Hi
I need to check whether the http_referer is the same site as the current site.
I have the following code
Dim strReferer As String
strReferer = Request.ServerVariables("HTTP_REFERER")
If strReferer.Contains(Request.ServerVariables("HTTP_HOST")) then
'do task
End If
This is throwing up an error saying - "Object reference not set to an instance of an object." and flagging the if line as the offending line of code.
Any ideas where I'm going wrong?
MY SOLUTION:
strReferer = "" & Request.ServerVariables("HTTP_REFERER")
Means the string always has a value even if it's nothing.
Because HTTP_REFERER is not always populated - only if you have clicked a link. So if you browse directly to a page, that header will be empty.
It's possible for Request.ServerVariables("HTTP_REFERER") to be null, so you should check for this when assigning the variable.
If Not String.IsNullOrEmpty(Request.ServerVariables("HTTP_REFERER"))
'do your stuff

Check if an Object exists in VBScript

I'm maintaining a Classic ASP app written in VB Script by an outside company long, long ago.
I have an array of imagefile paths, like so:
dim banners, arrKeys, i
set banners=CreateObject("Scripting.Dictionary")
banners.Add "banner1.jpg", "http://www.somelink.com"
banners.Add "banner2.jpg", "http://www.somelink.com"
banners.Add "banner3.jpg", "http://www.somelink.com"
This will exist ONLY on pages that have banner ads. There is some standard code that iterates through this list in an include file (common to all pages).
If Not banners Is Nothing then
' then loop through the Dictionary and make a list of image links
End if
The problem is that if banners is not instantiated on the page (it's not on all pages), I get a Can't find object error
What's the proper way to check if an object exists in VB Script?
#Atømix: Replace
If Not banners Is Nothing then
and use
If IsObject(banners) Then
Your other code you can then place into an include file and use it at the top of your pages to avoid unnecessary duplication.
#Cheran S: I tested my snippets above with Option Explicit on/off and didn't encounter errors for either version, regardless of whether Dim banners was there or not. :-)
IsObject could work, but IsEmpty might be a better option - it is specifically intended to check if a variable exists or has been initialised.
To summarize:
IsEmpty(var) will test if a variable exists (without Object Explicit), or is initialised
IsNull(var) will test if a variable has been assigned to Null
var Is Nothing will test if a variable has been Set to Nothing, but will throw an error if you try it on something that isn't an object
IsObject(var) will test if a variable is an object (and will apparently still return False if var is Empty).
If a variable is declared, but not initialized, its value will be Empty, which you can check for with the IsEmpty() function:
Dim banners
If IsEmpty(banners) Then
Response.Write "Yes"
Else
Response.Write "No"
End If
' Should result in "Yes" being written
banners will only be equal to Nothing if you explicitly assign it that value with Set banners = Nothing.
You will have problems, though, with this technique if you have Option Explicit turned on (which is the recommendation, but isn't always the case). In that case, if banners hasn't been Dimed and you try to test IsEmpty(banners), you will get a runtime error. If you don't have Option Explicit on, you shouldn't have any problems.
edit: I just saw this related question and answer which might help, too.
Somewhat related is IsMissing() to test if an optional parameter was passed, in this case an object, like this:
Sub FooBar(Optional oDoc As Object)
'if parameter is missing then simulate it
If IsMissing(oDoc) Then Dim oDoc as Object: oDoc = something
...
You need to have at least dim banners on every page.
Don't you have a head.asp or something included on every page?
Neither of IsEmpty, Is Object, IsNull work with the "Option Explicit" Setting, as stealthyninja above has misleadingly answered.
The single way i know is to 'hack' the 'Option Explicit' with the 'On Error Resume Next' setting, as Tristan Havelick nicely does it here:
Is there any way to check to see if a VBScript function is defined?

Resources