Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6023773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:03:19+00:00 2026-05-23T04:03:19+00:00

I am using LINQ and have a few properties thats DateTime? type. If i

  • 0

I am using LINQ and have a few properties thats DateTime? type.

If i now want to add the value from a textbox i cant seem to get this to work.

[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ScoringLastUpgrade", DbType="Date")]
public System.Nullable<System.DateTime> ScoringLastUpgrade

The textbox i use i have made sure with javascript that the format will be ‘2011-06-17’

But now when i try to do this:

myObject.ScoringLastUpgrade = Convert.ToDateTime(txtScoringUpgradeDate.Text).ToShortDateString();

I get this error: “Cannot convert type string to DateTime?”

How to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:03:20+00:00Added an answer on May 23, 2026 at 4:03 am

    Don’t convert it to string using ‘ToShortDateTimeString’, just set the result of Convert.ToDateTime:

    myObject.ScoringLastUpgrade = Convert.ToDateTime(txtScoringUpgradeDate.Text);
    

    Assuming you’ve done sufficient validation on txtScoringUpgradeDate.Text?

    My preference when dealing with these type conversions is to use the TryParse method, e.g.:

    DateTime date;
    if (DateTime.TryParse(txtScoringUpgradeDate.Text, out date))
        myObject.ScoringLastUpgrade = date;
    

    The Convert.ToDateTime, much like the explicit DateTime.Parse will throw an InvalidCastException when an excepional value occurs. It’s better to make your code fault tollerant then needlesly catch an exception.

    UPDATE: based on your last comment:

    You shouldn’t return DateTime.MinValue in this case, as MinValue is less than the supported min value of a datetime column. the CLR DateTime supports a date range down to 0000-01-01, whereas the SQL datetime (as well as the comparative CLR SqlDateTime type) supports a minimum value of 1753-01-01. As it as a nullable DateTime, you should set it to null:

    public static DateTime? ToNullableDateTime(this string date)
    {
        DateTime dateTime;
        return (DateTime.TryParse(date, out dateTime))
            ? (DateTime?)dateTime
            : null;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been using Linq to XML for a few hours and while it
I have been using linq in combination with entity framework for a few months
I have generated few classes using to LINQ to SQL one of them being
I have been using LINQ to SQL for years now, but this is the
Okay, so I am using linq and thus I have a few var types
i've been using Nhibernate with LINQ a fair bit now and i have a
When using linq and you have c.Sort() Is there any good inline way of
I'm using LINQ to SQL and C#. I have two LINQ classes: User and
How can I do a multi-level parent-child sort using Linq if I have a
Using LINQ to SQL, I have an Order class with a collection of OrderDetails.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.