views:

3206

answers:

8

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)
                        {
+7  A: 

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"]);
 DateTime endIssueDate = DateTime.Parse(r["EISS"]);

You could also use TryParse depending on your needs.

Chris Thompson
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."}
fzshah76
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
A: 

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

Jhonny D. Cano -Leftware-
+2  A: 

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 
}
Lobut
if(r["FISS"] == DBNull.Value)this one did it yes it is a null value, still testing:) Thank you
fzshah76
I saw your code and I added some suggestions in the 'EDIT' portion. What do you think?
Lobut
A: 

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
+2  A: 
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
A: 

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  A: 

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.

zvolkov
A: 

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