Click here to Skip to main content
15,896,269 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using a label named lblDateAssoc to display date, I convert this label into datetime datatype inside my blDoc.addDoc. Now the problem is it can't be null, everytime I leave the label blank/null the message error says that the string cannot be recognize as a valid datetime. My goal is to just simply leave the label empty when date is not specified. Thanks for helping.

here's the code that assigns the null value to my label:
C#
String dateT;
                if (lblDateAssoc.Text == "")
                {
                    dateT = " ";
                }
                else
                {
                    dateT = Convert.ToDateTime(lblDateAssoc.Text);
                }

Now here's the code the writes on the database:
VB
blDoc.addDoc(idSY, idUser, idS, txtTitle.Text.ToUpper(),
                       catergoryS,
                       Convert.ToDate(dateT),
                       dateC,
                       idA,
                    purpose,
                     txtAtt.Text,
                     DocStat,
                     randomN);
Posted
Updated 30-Aug-13 4:47am
v2
Comments
idenizeni 30-Aug-13 17:03pm    
The code where you say you are assigning your date to the label is actually pulling the date from the label. Are you trying to read the date from the label and pass it to your database? is this the issue or are you really having an issue setting the label text? If it's the database update... does your database table allow NULLs in this date field? Is it a date type on the database server?
Anand Kumar Prajapati 31-Aug-13 2:13am    
modify ur code like this -

string dateT=string.Empty;
if(string.IsNullOrEmpty(lblDateAssoc.Text)==false)
dateT = DateTime.TryParse(lblDateAssoc.Text);

hope this serve your purpose

To convert a string into DateTime variable, use DateTimme.TryParse() function:

http://msdn.microsoft.com/en-us/library/system.datetime.tryparse.aspx[^]
 
Share this answer
 
v3
You can also use IFormatProvider to parse your variable to datetime with the specified format.

Below is the sample code....
The format variable can have any format type like
string sFormat = ""dd/MM/yyyy";

C#
public string GetFormattedDate(string date, string sFormat)
        {
            string fdate = "";
            try
            {
                if (date != "")
                {
                    IFormatProvider f = new CultureInfo(DATECULTURE, false);
                    DateTime d = DateTime.Parse(date, f);
                    fdate = d.ToString(sFormat);
                }
            }
            catch
            {

            }
            return fdate;
        }
 
Share this answer
 
1. If your database field is datetime and allows null then only you can insert null values but here you inserting blank value. Null and blank spaces are different.So if you want to insert null value in field then it should b null value not blank space.Also use tryparse if you are converting datetime and also check server datetime format.For that convert in proper format also.
 
Share this answer
 
v2
I assume that .addDoc (eventually) wraps a SQL insert/update in which case handing null on to the DB shouldn't cause any problems providing you are allowed to modify the .addDoc method to accept Nullable<Date>. If that's the case then the following might be suitable.

C#
DateTime intermediate;
Nullable<datetime> dt = null;
string userInput = lblDateAssoc.Text;

if (!String.IsNullOrEmpty(userInput) &&
     DateTime.TryParse(userInput, out intermediate)) {

  dt = intermediate;

}

// Hand off to DB.
// Assumes that the DB write method can be rejigged to take a
// Nullable<datetime>
blDoc.addDoc(..., dt, ...);
 
Share this answer
 
v2

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