简体   繁体   中英

C# Load a date from a string into a datetimepicker

I'm trying to update an old c# program that I inherited along the way. Not big changes, just adding some new fields etc. I'm using Visual Studio 2008.

Specifically I'm trying to read a date time from an SQL Table and insert it into a DateTimePicker so the user can read / modify it on the screen.

I have tried a few different ways to do this but the code below seems like the most sensible.

I have read the date time string into a datetime variable from my SQL database.

I can display the value in a message box no problem but when I try and load it into the dateTimePicker control I get an exception.

The date displayed in the message box looks like this. 24/06/2015 12:00:00 AM (Australian Date format is correct and I don't care what the time is)

"Object Reference not set to an instance of an object" _COMPlusExceptionCode = -532459699

while (sqlDR.Read())
{
    this.textBoxFixedAmountStreetNumber.Text = sqlDR["lStreetNumber"].ToString() + sqlDR["sStreetNumberSuffix"].ToString();
    this.textBoxFixedAmountStreetName.Text = sqlDR["sStreet"].ToString();
    this.textBoxFixedAmountSuburb.Text = sqlDR["sTown"].ToString();
    DateTime dateString = Convert.ToDateTime(sqlDR["dFixedAmountEndDate"].ToString());
    MessageBox.Show("dFixedAmountEndDate=" + dateString, "Debug", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
    this.dateTimePickerFixedAmountEndDate.Value = dateString;
}

I'm probably the worlds oldest newbie because I do mostly sysadmin and never get to concentrate on a single programing language. :)

Any ideas on what I'm doing wrong is much appreciated.

Thanks David

This should do it:

var date_as string = "24/06/2015 12:00:00 AM";
DateTime date ;

DateTime.TryParse(d, out date);
// date now holds your date

Edit :
As other comments and Matt's answer suggest, you don't really need to parse a string (so the above even though correct, is probably not what you want).

If you do that you can use the bool result = DateTime.TryParse(d, out date); and if the result is false assign a fake date (or better, when you define date, you can do: DateTime date = new DateTime(); so if the parsing fails, you'll still have a valid date.

You can achieve the same by testing for null if you go with the other approach. Either way, you should be aware of the perils :)

This line is bad:

DateTime dateString = Convert.ToDateTime(sqlDR["dFixedAmountEndDate"].ToString());

This is its replacement:

DateTime fixedAmountEndDate = (DateTime) sqlDR["dFixedAmountEndDate"];

Never convert to a string when you don't have to, and use names that make sense.

However, if you're getting "Object Reference not set to an instance of an object" - That's a NullReferenceException . Chances are that one of your SQL fields is nullable in the database and contains a null value. You need to test for that condition explicitly, as a DateTime can't be nullable, and neither can DateTimePicker.Value .

if (sqlDR["dFixedAmountEndDate"] == DBNull.Value)
    // do something.  Perhaps hide the field, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM