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 6111443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:37:51+00:00 2026-05-23T14:37:51+00:00

I have a function that is getting passed a String and a DataRow. The

  • 0

I have a function that is getting passed a String and a DataRow.

The String is a custom formatter. The idea is to do this

String.Format(passed_in_String, DataRow("ColumnINeed"))

The reason this is being done is at this point we have no idea what the column contains.

However, if the formatter is “{0:MM/dd/yyyy}” and the DataRow(“ColumnINeed”) is an integer containing 42, String.Format is returning: MM/dd/yyyy

In this situation I need it to throw an exception instead of returning nonsense.

Is there anyway to make String.Format throw an exception if the object does not match what the format string is expecting?

  • 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-23T14:37:52+00:00Added an answer on May 23, 2026 at 2:37 pm

    You need to become familiar with your data. If you’re in a situation where the data could truly be anything at anytime, you’re in a bad spot. Frankly, I rather doubt you are in that situation. At least, I’m extremely hopeful you are not.

    If it is exceptional for the data in the column to not be a date, then pull it out as a date. Don’t pull it out and hope it nicely formats to one, pull it out as one!

    DateTime myDate = (DateTime)datarow["MyColumn"]; // C#
    Dim myDate As DateTime = CType(datarow("MyColumn"), DateTime) 'VB
    

    Even if you find your data to be stringly typed, immediately parse it. Set the expectation that the value is a date in your code. When it isn’t, it will create an exception.

    Edit: Chris mentions a good alternate suggestion in the comments. You can also write

    DateTime myDate = datarow.Field<DateTime>("MyColumn");
    

    The benefit here is what if the column allowed nulls?

    DateTime? myDate = (DateTime?)datarow["MyColumn"]; // C#
    Dim myDate = CType(datarow("MyColumn"), DateTime?) 'VB
    

    Here, you may get “Specified Cast Not Valid” if row holds DBNull.

    DateTime? myDate = datarow.Field<DateTime?>("MyColumn"); // C#
    Dim myDate = datarow.Field(of DateTime?)("MyColumn") 'VB
    

    This alternate handles DBNull appropriately.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that gets a string passed to it. When testing the
I have a class that takes a string in this format: 000067000000000012620060324b38e2cab3353 , encrypts
I have a function that looks like this class NSNode { function insertAfter(NSNode $node)
I have a string passed into a function, I would like to compare the
I keep getting errors that my functions have been defined multiple times. Of course
So I have function that formats a date to coerce to given enum DateType{CURRENT,
In my Java code I have function that gets file from the client in
I have a function that gives me the following warning: [DCC Warning] filename.pas(6939): W1035
I have a function that gets x(a value) and xs(a list) and removes all
I have a function that takes, amongst others, a parameter declared as int privateCount

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.