Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Textbox - can't leave it blank?

Status
Not open for further replies.

net123

Programmer
Oct 18, 2002
167
US
I have a web form in which users input data into text boxes. There are a myriad of textboxes and most of them can be left blank. I have a date field (textbox) and when left blank, it gives me the following error message:


============================
Cast from string "" to type 'Date' is not valid.

Source Error:

Line 83:
Line 84: Sub ImageButton_Click(sender As Object, e As ImageClickEventArgs)
Line 85: If Add(txtAcct.Text, txtName.Text, txtAddr.Text, txtCity.Text, ddlState.SelectedItem.Text, ddlAType.SelectedItem.Text, txtADate.Text, txtCode.Text, txtComm1.Text, txtComm2.Text, ddlBy.SelectedItem.Text) > 0
Line 86: Message.Text = "New Account created successfully!"
Line 87: Else

Stack Trace:

[InvalidCastException: Cast from string "" to type 'Date' is not valid.]
Microsoft.VisualBasic.CompilerServices.DateType.FromString(String Value, CultureInfo culture)
Microsoft.VisualBasic.CompilerServices.DateType.FromString(String Value)
ASP.EnterCassAccount_aspx.ImageButton_Click(Object sender, ImageClickEventArgs e) in c:\inetpub\System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e)
System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1277
[/red]
===================================

Here is some of my code that might help:

=============================

Function AddCass(ByVal Acct As String, ByVal Name As String, ByVal _
Addr As String, ByVal City As String, ByVal State As String, _
ByVal AType As String, ByVal ADate As Date, _
ByVal Code As String, ByVal Comm1 As String, _
ByVal Comm2 As String, ByVal By As String) As Integer

...

sqlCommand.Parameters.Add("@ADate", System.Data.SqlDbType.SmallDateTime).Value = ADate
[/blue]
============================

I can't seem to figure out what is going wrong...
 
You can't store an empty string into a DateTime datatype (since "empty" isn't a valid date or time).

You'll need to either:
1. Have a separate boolean to indicate if the datetime is empty or not and write code to handle it

2. Use a different 3rd party control that allows empty values (returns a database null value when field left empty).

3. Assign a default value that will be recognized as representing "empty" and write code to handle it

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top