DateTime as empty String or null, How can i Do it?

G

Guest

hello, i have a textbox that represnts a date and i want to pare it to a
DateTime, but sometimes this textBox can be empty string and i want to send
it to the DataBase as null or as empty Date, how can i do it?

thanks
 
H

Hans Kesting

Gidi said:
hello, i have a textbox that represnts a date and i want to pare it to a
DateTime, but sometimes this textBox can be empty string and i want to send
it to the DataBase as null or as empty Date, how can i do it?

thanks

Try the SqlDateTime instead.

Hans Kesting
 
N

Nicholas Paldino [.NET/C# MVP]

Either that, or if you can use the beta of .NET 2.0, Nullable<DateTime>.

Hope this helps.
 
N

Nicholas Paldino [.NET/C# MVP]

Either that, or if you can use the beta of .NET 2.0, Nullable<DateTime>.

Hope this helps.
 
T

Tom Porterfield

Gidi said:
hello, i have a textbox that represnts a date and i want to pare it to a
DateTime, but sometimes this textBox can be empty string and i want to send
it to the DataBase as null or as empty Date, how can i do it?

Using the default constructor for DateTime will initialize it to
1/1/0001. In our systems this is not a business valid value. So if no
date was entered by the user, then we leave our DateTime as 1/1/1. In
our data layer we look at our DataTime objects and if found to be 1/1/1
then we send null to the database.
 
Z

Zürcher See

Are you looking for the null value of the DB? ---> System.DBNull.Value

textbox.Text==string.Empty?(object)DateTime.Parse(textbox.Text):(object)Syst
em.DBNull.Value;
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

I have a similar situation in a web app, so what I do is use a hidden field
to store the "real" date and the textbox ( which is what the user see) has
whatever text I decide, if the user change the date or set it for the first
time I change both the hidden field as well as the textbox, in this way the
used value which is stored invisible from the user is always a DateTime and
the textbox can display what ever you want.

Cheers,
 
A

Anders Borum

I suggest using the constants on the DateTime object to compare for nulls
(i.e. no value selected for that specific DataTime).

I'm using the DateTime.MinValue / DateTime.MaxValue constants in my APIs and
the programmers have found it to be quite intuitive.

For instance:

// Specifies start and end datetime values
CmsPage.StartDate = DateTime.MinValue;
CmsPage.EndDate = DateTime.MaxValue;

Give it a thought!
 
B

Bob Grommes

That's a great pattern, and I'm glad to see it in CLR 2.0; thanks for
pointing it out.

I've done something similar with a passive record container class that
passes values between tiers. This record container holds the field values
of a DB record as properties of appropriate CLR types, tracks whether a
field is NULL and throws an exception if you try to read the value when it's
NULL. So you end up with code like the following for a record container,
rc:

if (rc.IsNull("FirstName")) {
someTextBox.Value = "";
} else {
someTextBox.Value = rc.FirstName;
}

// or, going the other way:

if (someTextBox.Value.Length == 0) {
rc.SetNull("FirstName");
} else {
rc.FirstName = someTextBox.Value;
}

Database NULLs are a pain in the touche no matter how you handle them.

--Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top