473,326 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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

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
Nov 16 '05 #1
8 37957

"Gidi" <sh*****@hotmail.com.dontspam> wrote in message news:DB**********************************@microsof t.com...
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
Nov 16 '05 #2
Either that, or if you can use the beta of .NET 2.0, Nullable<DateTime>.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Hans Kesting" <ne***********@spamgourmet.com> wrote in message
news:Om**************@TK2MSFTNGP09.phx.gbl...

"Gidi" <sh*****@hotmail.com.dontspam> wrote in message
news:DB**********************************@microsof t.com...
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

Nov 16 '05 #3
Either that, or if you can use the beta of .NET 2.0, Nullable<DateTime>.

Hope this helps.
"Hans Kesting" <ne***********@spamgourmet.com> wrote in message
news:Om**************@TK2MSFTNGP09.phx.gbl...

"Gidi" <sh*****@hotmail.com.dontspam> wrote in message
news:DB**********************************@microsof t.com...
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


Nov 16 '05 #4
Gidi wrote:
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.
--
Tom Porterfield
Nov 16 '05 #5
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;

"Gidi" <sh*****@hotmail.com.dontspam> schrieb im Newsbeitrag
news:DB**********************************@microsof t.com...
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

Nov 16 '05 #6
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Hans Kesting" <ne***********@spamgourmet.com> wrote in message
news:Om**************@TK2MSFTNGP09.phx.gbl...

"Gidi" <sh*****@hotmail.com.dontspam> wrote in message

news:DB**********************************@microsof t.com...
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

Nov 16 '05 #7
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!

--
venlig hilsen / with regards
anders borum
--
Nov 16 '05 #8
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

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:eG**************@TK2MSFTNGP09.phx.gbl...
Either that, or if you can use the beta of .NET 2.0, Nullable<DateTime>.
Hope this helps.

Nov 16 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: VMI | last post by:
How can I validate a null string or an empty string in the same IF statement? If I use this: if (myString.Trim().Length <= 0 || myString == null) { //do stuff } I'll get a run-time error...
11
by: Dan Bass | last post by:
which one do you use and why? MyString == null || MyString == "" vs MyString == null || MyString.Length == 0
4
by: web1110 | last post by:
I have an array of of 5 string elements. I put values in 3 of them. Yet when I loop over them, I do not catch the empty string. The code output below does not include "Empty" stringx=new...
3
by: Asha | last post by:
greetings, i have some questions below, what are the differences between private string _strVal = string.Empty; and _strVal = null; does the string.Empty; allocate memory for it? how about...
1
by: Tan | last post by:
Hello I have a property date in the object data sourse which bind to Detailview, when this date property is null , it will display the date 01/01/0001, how can I convert it to empty string or...
5
by: Tan | last post by:
I am using ObjectDataSource bind to DetailView which will contain one of the column with the value type is date. whenver the value of this column is empty . then it will be display the date in the...
26
by: Neville Lang | last post by:
Hi all, I am having a memory blank at the moment. I have been writing in C# for a number of years and now need to do something in VB.NET, so forgive me such a primitive question. In C#, I...
3
by: Sugandh Jain | last post by:
Hi, I am using property like this.. private DateTime? _propName; public DateTime? PropName { get { return _propName; }
2
by: Jay | last post by:
I have a SQL Server table with nvarchar type column which has not null constraint. I am inserting empty string ("") from Java to the table column. When I export this table into .csv file using bcp...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.