Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
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
Old May 17th, 2007, 04:24 PM
Authorized User
Join Date: Nov 2005
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
Default Prevent converting empty textbox string to date.

I'm creating a form to search for records via TextBoxes for Name, Number, and Date. If all boxes are empty, I want the DataSource to return 0 records. When the form loads, all the text boxes are empty, and I get error:
Conversion from string "" to type 'Date' is not valid.
Old May 17th, 2007, 04:50 PM
Wrox Author
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons

That is probably because you are calling your code on Page_Load; move the code to a seperate method and have a button click or something call your code. Also, this is a bug. You need to test for the absence of data else your script will bomb everytime someone tries to search and they leave the date field blank.

if(this.txt.Text == String.Empty)
  //do something

Alternatively you can do

if(this.txt.Text == "")
   //do something

================================================== =========
Read this if you want to know how to get a correct reply for your question:
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
================================================== =========
Why can't Programmers, program??
================================================== =========
Old May 18th, 2007, 06:52 PM
Authorized User
Join Date: Apr 2005
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts

You could also use the method "String.IsNullOrEmpty(value)" to check. It will return true if there is null or empty or return false if it isn't. This works good if you have a null as I believe the other way throws an exception if there is a null. I play with nulls a lot. :)

Similar Threads
Thread Thread Starter Forum Replies Last Post
empty string Vince_421 VB Databases Basics 2 October 29th, 2008 03:24 PM
Problem converting string to Date. akhilhp ASP.NET 2.0 Basics 2 July 6th, 2008 08:02 AM
Is String.Empty spinout ASP.NET 2.0 Basics 6 July 10th, 2007 05:55 AM
Saxon - Prevent closing of empty elements? tripecac XSLT 3 April 27th, 2007 08:22 AM
Converting to character from date string denver Access VBA 7 June 9th, 2006 07:30 AM

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