Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

How can I set the null value into default value from code behind of asp.net?

(using c#....default value is 1/1/2011)

Please Help.
Posted
Updated 3-Oct-11 22:49pm
v3
Comments
Sergey Alexandrovich Kryukov 4-Oct-11 1:03am    
It depends; default for what?
--SA
skkworld 4-Oct-11 1:10am    
default means show the datetime of (1/1/2011)
Sergey Alexandrovich Kryukov 4-Oct-11 1:18am    
Sorry, I got it already; please see my solution -- it works.
--SA
Dalek Dave 4-Oct-11 4:49am    
Edited for Readability.

hey..
first declare nullable datetime variable and then use if statement to check for ur default value. if it is yes then set it to null...
C#
Nullable<datetime> mydate;


if ((Txtdate.Value == "1/1/2011") || (Txtdate.Value == "01/01/2011"))
        {
           mydate  = null;
        }
        else
        {
           mydate  = Convert.ToDateTime(Txtdate.Value);
        }


hope this help u..
 
Share this answer
 
v2

in my table field createdon shown in the date time format like 5/5/2011..in this field any of the value null means...it should be display into 1/1/2011


with this regard you can write your database query for your solution..

like while selecting your column with datatime you can write like this..

SELECT isnull(createdon,'01/01/2011') as CreatedOn FROM "YOUR TABLE NAME"

this will check first that that filed is null or not, if it is null then select your value like 01/01/2011 or if it is not null then its value will be your actual value...
for more detail on isnull see this..
<a href="http://msdn.microsoft.com/en-us/library/ms184325.aspx">ISNULL</a>[<a href="http://msdn.microsoft.com/en-us/library/ms184325.aspx" target="_blank" title="New Window">^</a>]
or
<a href="http://www.w3schools.com/sql/sql_isnull.asp">ISNULL2</a>[<a href="http://www.w3schools.com/sql/sql_isnull.asp" target="_blank" title="New Window">^</a>]

OR
You can use a CASE..WHEN in SELECT Query also...
for more detail on CASE..WHEN see This
<a href="http://msdn.microsoft.com/en-us/library/ms181765.aspx">CASE..WHEN</a>[<a href="http://msdn.microsoft.com/en-us/library/ms181765.aspx" target="_blank" title="New Window">^</a>]<br />

<a href="http://www.1keydata.com/sql/sql-case.html">CASE..WHEN 2</a>[<a href="http://www.1keydata.com/sql/sql-case.html" target="_blank" title="New Window">^</a>]<br />

<a href="http://www.java2s.com/Code/SQL/Flow-Control/UseCASEWHENstatementinSELECT.htm">CASE..WHENE 3</a>[<a href="http://www.java2s.com/Code/SQL/Flow-Control/UseCASEWHENstatementinSELECT.htm" target="_blank" title="New Window">^</a>]
 
Share this answer
 
v2
Hello friend..
first of all you need to clear you question, means what you want. you want null value in codebehind code or to store null value to your database.
and also put some code, so its good for us to understand your problem.

mean while try this...

in c#

for giving null value its not accept directly, but for this you need to define variable like this.

C#
int a = 0 // valid

int a = null // gives error

//but for assining null to it you need to declare it as

int? a = null //valid


i hope its help to you to solve your problem. if its use full for you please accept as ans and also rate it.
 
Share this answer
 
v2
Comments
skkworld 4-Oct-11 1:20am    
in my table datime showing null value...that null value is change into deafault value like 1/1/2000 ....
Simon Bang Terkildsen 4-Oct-11 3:04am    
My 5, Though in the future I suggest you use the type that the question is mentioning, in this case DateTime
The type DateTime is not a reference type, it cannot be null. Change is into nullable type:

C#
class Something {
    System.DateTime? MyTime {
        get { return fMyTime; }
        set {
            if (value == null)
               fMyTime = defaultTime;
            else
               fMyTime = (System.DateTime)value;
        }
    }
    System.DateTime fMyTime;
    static readonly System.DateTime defaultTime = new System.DateTime(2011, 1, 1);
}


The character '?' after System.DateTime indicates nullable type. See:
http://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx[^],
http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^].

—SA
 
Share this answer
 
v2
Comments
skkworld 4-Oct-11 1:31am    
in my table field createdon shown in the date time format like 5/5/2011..in this field any of the value null means...it should be display into 1/1/2011
Simon Bang Terkildsen 4-Oct-11 3:13am    
I suggest you do as SA and the other two solution providers has suggested, that is make your DateTime nullable, and then default to 1/1/2011 when your date is null after retrieving it from the database. If you want to do this in sql then you need to state which database you're using, in PL/SQL you would use decode
Simon Bang Terkildsen 4-Oct-11 3:05am    
My five

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