silomonline
New Member
On a form I have several date-field text boxes that the user may not fill in (not required). If I do an 'UPDATE table SET datefld=txtDatefld.text WHERE id=@id', I get a 'Cast from String ('') to Date is not Valid' when the field is left blank.<BR>What magic do I need to do to txtDatefld.text to get it to save a a blank date?You need to test whether the value is null, and replace the value with a null, the database is trying to cast an empty string into a date. I would also recommend looking at using a validator to check whether the date is valid, but this will solve your problem as long as you enter correct dates.<BR><BR>see http://www.aspsmith.com for info on regular expressions, and validators<BR><BR>for example<BR>if (txtDateField.text.length == 0)<BR>{<BR>update table set datefld = null where id = @id<BR>}<BR>else<BR>{<BR>update table set datefld = txtDatefld.text where id = @id<BR>}