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

  • Home
  • SEARCH
  • 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 8907587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:57:18+00:00 2026-06-15T02:57:18+00:00

I got a problem. This my code use a LINQ. var resultCases = from

  • 0

I got a problem. This my code use a LINQ.

var resultCases = from row2 in dtCases.AsEnumerable()
                  where row2.Field<int>("caseID") == caseID2
                  select new
                  {
                              caseName = row2["caseName"].ToString(),
                              caseCourtPlace = row2["caseCourtPlace"].ToString(),
                              caseCourtRef = row2["caseCourtRef"].ToString(),
                              caseOfficeRef = row2["caseOfficeRef"].ToString(),
                              effectiveDate = ((DateTime)row2["caseEffectiveDate"]),
                              closedDate = ((DateTime)row2["caseClosedDate"]),
                              caseFolderPath = row2["casesFolderPath"].ToString(),
                              category = row2["categoryName"].ToString(),
                              department = row2["departmentName"].ToString(),
                              empName = row2["empName"].ToString(),
                              judgeName = row2["judgeName"].ToString(),
                              asName = row2["asCasesName"].ToString(),
                  };

If closedDate or effectiveDate return DBnull.Value, I get

InvalidCastException was unhandled – Specified cast is not valid.

So how can I prevent this error?

  • 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-06-15T02:57:19+00:00Added an answer on June 15, 2026 at 2:57 am

    tldr; The issue is two-fold: DataTable uses DBNull.Value to represent “null” values and neither DBNull.Value or null are castable to DateTime.

    The Field<T> extension method was added later to make dealing with DBNull and Nullable/reference types with null values much easier; it also hides the conversion behind a strongly-typed signature. These LINQ to DataSet extension method knows how to map DBNull.Value to null as appropriate.

    Because of this using row.Field<DateTime?>("caseEffectiveDate") will return either the DateTime? with a value (if the query returned a value) or null, and it may also throw an exception if the server returned an incompatible value – but it will never return DBNull.Value.

    However, the standard row["caseEffectiveDate"] will return a DateTime value (or some other type) or DBValue.Null which is not cast-able to DateTime? (much less a DateTime) and results in the error described.

    Here is how to minimally reproduce this exception:

    object v = DBNull.Value;
    DateTime dt = (DateTime)v;
    

    However, it is fine to convert a null to a DateTime?:

    object v = null;
    DateTime? maybeDt = (DateTime?)v;
    

    And then trivial to coalesce it away to DateTime if required:

    object v = null;
    DateTime? dt = (DateTime?)v ?? DateTime.MinValue;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got a problem with this linq query: from PersistedFileInfo fi in m_Database from
ok i got this problem. i have this routes: (code bit change) File:/home/dotcloud/current/config/routes.js exports.routes
I got this code today that works for jQuery v7. The problem is that
My Problem is the following. I got this code and i guess a corrupt
I've got another problem in the same code... I'm getting this error: initialization method
It's a really simple problem. I've got HTML code like this : <div> <img
Ive got this problem: Line 20 (LOOT_FromContainer(container) I need that to use as Invoke
I got this problem with AVAudioRecorder not working for me, at least from what
i have a linq to dataTable query like this: `var ShowResult = from r
I'm working on some code, and got stuck on this stored procedure problem... Have

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.