Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old July 8th, 2007, 08:44 PM
Authorized User
 
Join Date: Sep 2004
Location: Sydney, NSW, Australia.
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
Default Is String.Empty

Hi Folks

Just wondering if anyone knows of any problem in using "Is String.Empty" to test if a variable is empty.

I currently have the following block of code:

Code:
Dim strDateToDisplay
strDateToDisplay = Request.Form("selectDate")
If strDateToDisplay Is String.Empty Then
  strDateToDisplay = DateTime.Now.ToString("yyyy-MM-dd")
Else
  strDateToDisplay = strDateToDisplay
End If
Now, the general idea of this code is to check whether a user has selected a date from a form drop down box. If they have, then the data for that date is returned to the user. If they haven't, the data for the current date is returned to the user by default.

Currently, if a user selects a date from the drop down box, the data is returned as expected. However if a user doesn't select a date from the drop down box then nothing is returned by default (when data for the current date should be returned).

Thanks for your help! :)


  #2 (permalink)  
Old July 9th, 2007, 08:10 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I suspect that your if test is always failing.

From the MSDN docs: "The Is operator determines if two object references refer to the same object. However, it does not perform value comparisons. If object1 and object2 both refer to the same object, result is True; if they do not, result is False." (http://msdn2.microsoft.com/en-us/library/kb136x1y(VS.71).aspx)

So in this case, you are test whether strDateToDisplay and String.Empty are the same object reference, which are definitely not. Instead you need to use a regular value comparison:

    strDateToDisplay = String.Empty

-Peter
  #3 (permalink)  
Old July 9th, 2007, 08:47 AM
Authorized User
 
Join Date: Sep 2004
Location: Sydney, NSW, Australia.
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hi Peter, thanks for the info.

I tried changing my code to...

Code:
If strDateToDisplay = String.Empty Then
  strDateToDisplay = DateTime.Now.ToString("yyyy-MM-dd")
Else
  strDateToDisplay = strDateToDisplay
End If
It still doesn't seem to work though. I tried adding a Response.Write(strDateToDisplay) inside the If part of the statement but it doesn't return a value which would indicate to me that the If part of the statement is still failing.

  #4 (permalink)  
Old July 9th, 2007, 09:02 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I just noticed... why are you using "Request.Form("selectDate")" to get the value? This is classic ASP style and may be the source of the problem and definitely won't work if this is in a user control. You should be using the server control to get the value. Also, is it possible that the value coming back is a whitespace string?

-Peter
  #5 (permalink)  
Old July 9th, 2007, 09:15 AM
Authorized User
 
Join Date: Sep 2004
Location: Sydney, NSW, Australia.
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by planoie
I just noticed... why are you using "Request.Form("selectDate")" to get the value?
Because I'm an old school classic ASP coder trying to learn ASP.net who doesn't know any other way to achieve what I'm trying to do other than by using Request.Form(""). :D

What should I be using?


Quote:
quote:
This is classic ASP style and may be the source of the problem and definitely won't work if this is in a user control. You should be using the server control to get the value. Also, is it possible that the value coming back is a whitespace string?
The thing is, that part of the code works. If a user selects an option from the dropdown box on my form, the Else part of the statement executes and returns the data relevant to their selection.

It's only if they don't select anything from the dropdown box (i.e. when the page loads before they have a chance to select an option) I want the code to return data specific to the current date as the default.

This is the part which doesn't seem to work (by trying to test if the string is empty, and therefore no selection has been made, so default should be used).
  #6 (permalink)  
Old July 9th, 2007, 11:57 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Aha! Ok. What's going is this:

In classic ASP if you did this:

   strMystring = Request.Form("somekey")

and it was the first time the page loaded, strMystring would be == ""

HOWEVER, in .NET, this results in strMystring == null/Nothing.

So your test is failing until the user makes a selection because the first page load there is no form value (null) which doesn't equal string.empty.

Try this:

    If String.IsNullOrEmpty(strDateToDisplay) Then

This will fix the immediate problem. However, there is a larger one you need to address. You need to use the server controls on your page and not use the Request.Form collection. The page framework handles all the parsing of the form post values, you can use the typed controls that you create.

If you are using the page designer and have dropped controls on the page, just double click on the control and you will get the default event for that control (button.Click, dropdownlist.SelectedIndexChanged) and VS will create the event handler for you.

-Peter
  #7 (permalink)  
Old July 10th, 2007, 05:55 AM
Authorized User
 
Join Date: Sep 2004
Location: Sydney, NSW, Australia.
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by planoie
You need to use the server controls on your page and not use the Request.Form collection. The page framework handles all the parsing of the form post values, you can use the typed controls that you create.
Thanks again for your help Peter.

After all that, I discovered that the reason the If statement wasn't working was because it was within the Button_Click block of code, and not the Page_Load block. D'oh!

In the end, I actually used "If Not Page.IsPostBack Then" to achieve the result I was after (don't know why I didn't think of that to begin with given what I as trying to do).

I also had a good think about your suggestion re cleaning up my code to use valid ASP.net controls and not classic ASP objects.

I replaced all of my classic HTML code ...

Code:
<select name="date">
 <option>10 July</option>
 <option>11 July</option>
</select>
... with valid ASP.net server controls, data bound to SQL ...

Code:
<asp:DropDownList runat="server" id="date"></asp:DropDownList>
Not only does this make my code more valid, it's much cleaner now too because I don't have asp:Repeater controls wrapped around classic HTML. You're suggestion actually helped me solve another problem I was having on another page too so all round a great result.

Thanks again for your help!


Similar Threads
Thread Thread Starter Forum Replies Last Post
empty string Vince_421 VB Databases Basics 2 October 29th, 2008 03:24 PM
isNot nothing vs. string.empty julianfraser BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 1 June 8th, 2008 02:00 PM
Difference between null and empty string shoakat Classic ASP Databases 4 September 10th, 2004 11:40 AM
Checking for empty string richw32001 General .NET 1 August 8th, 2004 01:37 PM
Convert empty string to double adriant42 Excel VBA 2 June 8th, 2004 04:22 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.