Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
not converted date to "0"
C#
if (txtDate.Text.ToString() == "")
               txtDate.Text = "0";
Posted
Updated 14-May-12 1:20am
v3
Comments
Prasad_Kulkarni 14-May-12 7:21am    
Not clear. Please elaborate some more.
tanweer 14-May-12 7:22am    
what is your question? what do you want to do?
2011999 14-May-12 7:26am    
Empty date field (how to submit the empty date field)i got error convertion problem.
AhsanS 14-May-12 7:33am    
First of all no need to cast ToString.
Secondly check for empty string like this
if (string.IsNullOrEmpty(txtDate.Text))
thirdly what do you want?
If you want to assign it the text than you are doing it the right way
i.e. txtDate.Text = "0";

If you have to allow NULL to your date field you can go like this..

in UPDATE QUERY OR INSERT QUERY IN SORT ANY SQL QUERY you can put "case when" for inserting or updating date value to blank.

like this.

SQL
UPDATE TABLENAME SET ENTRYDATE = CASE WHEN @DATE = '' THEN NULL ELSE @DATE END


OR FOR INSERT QUERY

SQL
INSERT INTO TABLENAME (USERNAME,ENTRYDATE) VALUES ('TEJASVAISHNAV',CASE WHEN @DATE = '' THEN NULL ELSE @DATE END)
 
Share this answer
 
Comments
Ed Nutting 14-May-12 7:42am    
A valid solution and helpful SQL - my 5+. I tried to provide a more code/LINQ to SQL based approach given OP's use of C#/ASP.Net.

Ed
Maciej Los 14-May-12 7:50am    
Good answer, my 5!
Hi there,

When you say "empty" I presume you mean blank, no date at all, or in proper terms - null? If you want to submit a null date value to your databse, make sure your data column allows nulls then set the value to null. Do no try to pass in empty strings - this wont work - unless you are using a varchar or nchar field for stroing adtes which is bad practice anyway...

Without your code this is very hard but your code should look something like this:
DateTime? DateToSubmit = null;
if(!string.IsNullOrEmpty(txtDate.text))
{
    DateTime.TryParse(txtDate.Text, out DateToSubmit);
}
//Submit DateToSubmit to your database
//If field is DateTime
MyDataObject.MyDate = DateToSubmit;
//or if field is string
MyDataObject.MyDate = DateToSubmit.HasValue ? DateToSubmit.Value.ToString() : "";


Hope this helps,
Ed
 
Share this answer
 
If your table accept NULL value, set it DBNULL.Value.
If not, set default date, like '1900-01-01'.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900