Any Reason Why IsNumeric() Fails On A Number? - asp.net

I currently have this line of code which has been working for the past 6 months:
If IsNumeric(txtProductID.Text) Then
...do stuff
Else
Dim msg As String = "Error!"
End If
All of the sudden, no matter what kind of entry is put in txtProductID (including plain numbers), it fails! Is there reason for me to be going crazy over this?

Kind of a shot in the dark, but one thing to watch for is that maybe someone wrote a private method called IsNumeric within the same class. Are you sure that the code above is executing Microsoft.VisualBasic.IsNumeric()? If you put your cursor on IsNumeric and hit F12 where does the definition point to?

Try Trim()ing the string before passing it into the function. In addition, rather than using a VB-specific function like IsNumeric, you might try an approach like this:
Dim input as Integer
If Integer.TryParse(txtProductID.Text, input) Then
....do stuff with input
Else
Dim msg as String = "Error!"
End if
If your number is a decimal number, there are corresponding functions on Double and Single as well.
As to the particular reason that IsNumeric is failing, I couldn't tell you. I can tell you, though, that I've always found it helpful to stick to BCL-compliant functions that are language-agnostic rather than language-specific, like IsNumeric, Str, etc.

ugh... i'm an idiot... thanks for your help guys, but apparently i was clearing my whole form before accepting input, so "" will never pass as "IsNumeric". Please don't look at this question again. I feel ill.
Thanks again for your help.

Related

C# Save Date on event, tell me how many days it has been

So I have been working on this for quiet a while, I want to basically have something on my form2 save a datetime in Settings. Default["Day Recieved"]; on a event (button click) and I want it tell me how many days it has been on a textbox in form1. I have tried several approaches, but I cant seem to assign it properly as when I try I get a message saying that I cant convert it, even if I convert it to a datetime type, I made a whole class trying to get it to work properly, one time I had too much converting going on or something and the string passed to textbox was "TestApp.Settings+GetDate" I think that I just cant figure out how to do the right way, I have plenty of more ideas to try to get it to work, but I think there very bad ways, at least in practice, how should one do this properly without making a fool of themselves?
(I am aware I made this kinda hard to read, all I can think about is code right now and clear sentences escape my thought, since I been on this for about 8 hours, so please go easy on me, I am a nice guy :D)
I have found out how to do it, indeed i was over complicating it and have learned my lesson,
Form2:
string when = System.DateTime.UtcNow.Date.AddDays(30).ToString();
Form1.passwhen = when;
Back on form one, declare the global string for passwhen and then do:
textBox.Text = passwhen;
Settings.Default["Day"] = textBox10;
Settings.Default.Save();
I still need to clean it up and make it load the saved setting on load, but this is simple and worked just fine, i was confused because once i save the datetime in settings, VS was saying that System.DateTime and DateTime were two different types and that i couldn't explicitly convert it.

A couple ASP questions

Can anyone tell me what each of these are doing?
<%call buildBanner()%>
What I think this one is doing is calling a Method But I'm not that familiar with ASP.
Dim nInstallID : nInstallID = getParam("InstallID")
This I'm not quite sure, But from What I gathered it's a string. But I'm not sure what the ":" does or is doing.
The "call buildBanner()" is calling a function from somewhere else in your code. The function could be on the same page or it could be in an "include" file.
The ":" is just a way to separate commands on the same line. Normally you would put the two parts on two separate lines, but this is a shortcut way to use one line. Some people like to declare and initialize the variable on the same line - something you can't do in a single statement in Classic ASP.

ASP.Net - Function output shown before function called

I have the following line of code in ASP.Net (VB)
Response.Write("<td class=""tblRow""><strong>" & ITServiceRow.NAME & " </strong><br>" & funcRAGColour(ITServiceRow.RAGSTATUS) & Environment.NewLine)
This should output the Name from ITServiceRow.NAME followed by the result of the function funcRAGColour.
However this is not the case. ASP.Net is outputting the value of the function funcRAGColour. first followed by the value of ITServiceRow.NAME.
Just trying to understand why this might be happening? If I replace the function with static text it executes fine, but when I put the function in it outputs the function result immediately before the name.
The image here, in yellow shows the full output that comes from the function, it is shown before everything else?
Am I missing something obvious here?
Try using String.Format instead to guarantee placement.
Response.Write(string.Format("<td class=""tblRow""><strong>{0}</strong><br />{1}{2}</td>",funcRAGColour(ITServiceRow.RAGSTATUS),Environment.NewLine))
Always do whatever you can to avoid string concatenation. String concatenation is tough on a system and uses much more memory and resources to be garbage collected than you think because it's actually far more complicated. String.Format and StringBuilder help get around this.
I am very suspect of the function funcRAGColour() itself though and think that is the problem. My guess is the function is not returning the output as a string, but instead is using Response.Write() to output it's result. That would cause it's value to appear first since it is called while the string is being assembled.
Keep in mind, Response.Write is NOT the way to do things in ASP.Net. It was need in classic ASP, but ASP.Net has HtmlTextWriters that can be used during the rendering process, controls for result placement, etc.. It's the old school, non object-oriented way of doing things that can get into trouble.

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?

check string for nothing and then for certain value in 1 line?

I always seem to see if a string (querystring value usually) has a value but first I have to check that it is not nothing first so I end up with 2 if then statements - am I missing somethign here - there has to be a better way to do this:
If Not String.IsNullOrEmpty(myString) Then
If CBool(myString) Then
//code
End If
End If
In VB.Net, And does not short-circuit, but AndAlso does.
(same for Or and OrElse)
So your code should look something like
If Not String.IsNullOrEmpty(myString) AndAlso CBool(myString) Then
....
End If
The boolean "and" operator in your language of choice? Conditionals generally short-circuit so if the first one fails, the second won't run/error.
AndAlso (as mentioned) is the most general purpose answer. But, you could also use the various TryParse methods, which will make code like this:
Dim b as Boolean
If Boolean.TryParse(myString, b) AndAlso b Then
End If
Bonus, it'll save you from the FormatException when someone sends in "blah" in your querystring.
FYI this type of code fits perfect as an "extension method". In short this means that you can extend string with a method providing your own code.
Look it up :)
Use the following if using .NET 2.0 or greater:
String.IsNullOrEmpty(thestringobject)
Will evaluate to True or False depending.
I usually create another function in a Utility Class that does this check and also checks the trim/length so I know if the string object is null, empty, or a bunch of blanks.

Resources