0

I'm trying to pull some data from my Database, but when it comes to Datetime I get an

InvalidCastException

This is my Database (SQL SERVER)

Create table Timeline(
start date,
end date
)

table Timeline

start(date)            end(date)
03/07/2020             NULL
NULL                   10/07/2020
15/07/2020             25/07/2020

This is my query

SELECT Timeline.start, Timeline.end FROM Timeline

This is my c# code

public class Timeline
{
    public DateTime? startDate;
    public DateTime? endDate;


    public static Timeline fromRow(DataRow r)
    {
        Timeline a = new Timeline();
        
        a.startDate = r.Field<DateTime?>("start");
        a.endDate = r.Field<DateTime?>("end");

        return a;
    }
}

When I pull a NULL value it works, but when I pull a real date like 03/07/2020 It gives me InvalidCastException. Is there a way to check the value before putting it into the variable?

8
  • Is the field in the database a string by any chance? Commented Jul 3, 2020 at 14:45
  • Which database is this? How is the table defined? Commented Jul 3, 2020 at 14:46
  • I will recreate my Database Commented Jul 3, 2020 at 14:46
  • 1
    Friendly FYI your naming conventions are off C# uses PascalCase over camelCase for methods classes and public fields and properties. For one I am curious how the datatable is being setup here, if the column is being set as type string could explain why cast isnt happening here, just for debugging sake try parsing the result i.e a.startDate = DateTime.TryParse(r.Field<string>("start"), out var sd) ? sd : (DateTime?)null; ... Commented Jul 3, 2020 at 15:16
  • 1
    Glad I could help I will submit this as an answer because I believe there might be value to it for others :) Commented Jul 3, 2020 at 17:27

1 Answer 1

1

This is because the column type does not match up with what you are trying to cast to here, my guess is it is being referenced as a varchar type constructor public DataColumn (string columnName), which is fine we can just parse the date safely as a string, you may lose a few cycles on this but this is not 1995 anymore. If you want to cast safely than offer the column the correct type so constructor public DataColumn (string columnName, Type type)


// Change Names to match C# nameing conventions
public class TimeLine
{
    // Since this class is just a strongly typed object for rows of your table values should not be able to change.
    public DateTime? StartDate { get; private set; }
    public DateTime? EndDate { get; private set; }


    public static TimeLine FromRow(DataRow r)
    {
        // Casting will depend if we know the DataColumns type, if we dont we can just parse the string value
        return new TimeLine
        {
            StartDate = DateTime.TryParse(r.Field<string>("start"), out var sd) ? sd : (DateTime?)null,
            EndDate = DateTime.TryParse(r.Field<string>("end"), out var ed) ? ed : (DateTime?)null,
        };
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.