21

How can I get DateTime value in C# from row, the current code is giving me error any help is appreciated, the data is coming in from progress database:

foreach (DataRow r in ds.Tables[0].Rows)
{
    string prodCode = r["PRD-CDE"].ToString();
    statCode = r["STAT"].ToString();
    DateTime firstIssueDate = (DateTime)(r["FISS"]); 
    DateTime endIssueDate = (DateTime)(r["EISS"]);
    if(endIssueDate > DateTime.Now)
    { /*do some thing...*/}
    else {/*user invalid...*/}
}

there are two ways used in getting date convert and pars, thank you all for the help

Final working Code snippet:

foreach (DataRow r in ds.Tables[0].Rows)
            {
                string prodCode = r["PRD-CDE"].ToString(),statCode = r["STAT"].ToString();
               // r.<DateTime?>("FISS");
                if (r["FISS"] != DBNull.Value)
                {
                    DateTime firstIssueDate = Convert.ToDateTime(r["FISS"]);
                    if (r["EISS"] != DBNull.Value)
                    {
                        DateTime endIssueDate = DateTime.Parse(r["EISS"].ToString());
                        if (endIssueDate > DateTime.Now)
                        {
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Developer
  • 2,987
  • 10
  • 40
  • 51

8 Answers8

45

This is just a guess but if the corresponding type in the database is DateTime, could you check if the column is nullable?

If so you may want to do a check r["column"] == DBNull.Value and then pass it to a nullable DateTime? Field.

Or even easier:

row.Field<DateTime?>("column")

If it isn't then yeah, Convert.ToDateTime() or something else should do it.

EDIT:

I see your final code there but is there any chance you want to do this:

DateTime? firstIssueDate = r.Field<DateTime?>("fiss"); 
DateTime? endIssueDate = r.Field<DateTime?>("eiss"); 

if (firstIssueDate.HasValue && endIssueDate.HasValue) 
{ 
    firstIssueDate.Value // blah blah 
    endIssueDate.Value // blah blah 
}
Robert Luong
  • 522
  • 4
  • 5
12

I would recommend using DateTime.Parse() if the row is returning a string for that index.

 string prodCode = r["PRD-CDE"].ToString(),statCode = r["STAT"].ToString();
 DateTime firstIssueDate = DateTime.Parse(r["FISS"].ToString());
 DateTime endIssueDate = DateTime.Parse(r["EISS"].ToString());

You could also use TryParse depending on your needs.

mcNux
  • 1,472
  • 1
  • 15
  • 13
Chris Thompson
  • 16,203
  • 9
  • 45
  • 62
  • Error: Argument '1': cannot convert from 'object' to 'string' after tyrying following code: DateTime firstIssueDate = DateTime.Parse(r["FISS"]); even tried: DateTime firstIssueDate = DateTime.Parse(r["FISS"].ToString()); Error: ex = {"String was not recognized as a valid DateTime."} – Developer Jul 09 '09 at 20:23
  • Is there any way you could show us what r["FISS"].ToString() looks like? Perhaps it's a nullable type as someone mentioned. – Chris Thompson Jul 09 '09 at 20:32
5

If you want to use a default value (such as DateTime.MinValue), rather than null (DateTime?) or DBNull, you could do this:

var firstIssueDate = r["FISS"] as DateTime? ?? DateTime.MinValue;
var endIssueDate = r["EISS"] as DateTime? ?? DateTime.MinValue;
brianary
  • 8,996
  • 2
  • 35
  • 29
2
foreach (DataRow r in ds.Tables[0].Rows)
{
    string prodCode = r["PRD-CDE"].ToString();
    string statCode = r["STAT"].ToString();
    DateTime firstIssueDate = DateTime.Parse((r["FISS"]).ToString()); 
    DateTime endIssueDate = DateTime.Parse((r["EISS"]).ToString());
    if(endIssueDate > DateTime.Now)
    { /*do some thing...*/}
    else {/*user invalid...*/}
}

This should compile and may work for you. Though it is certainly not performing any error checking that you should do for production code. Also look into DateTime.TryParse and you may to look into adding a IFormatProvider to ensure the format is parsed as expected.

Timothy Carter
  • 15,459
  • 7
  • 44
  • 62
1

First of all, do r["FISS"].GetType() and print it to console (or pause and look at it in the debugger). If it says it's a String, then most of the above advices will help. If it says something else, please come back and update your question.

Andriy Volkov
  • 18,653
  • 9
  • 68
  • 83
0

As a side answer, you could use also the static function Convert.ToDateTime

Jhonny D. Cano -Leftware-
  • 17,663
  • 14
  • 81
  • 103
0

DateTime.Parse(r["FISS"].ToString()) is the way to go, but it throws a "String was not recognized as a valid DateTime" error. Could you show the actual string in the r["FISS"] column, it might be a internationalisation problem....

Colin
  • 10,630
  • 28
  • 36
0

If you have a DateTime string with a special format (not any standard .NET DateTime format) that needs to be converted to .NET DateTime type, you can use DateTime.ParseExact() method.

Please see the MSDN document for more details including examples.

If you have multiple formats to parse, try DateTime.ParseExact Method (String, String[], IFormatProvider, DateTimeStyles)

Chansik Im
  • 1,473
  • 8
  • 13