Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
How i can convert t-sql datetime to c# datetime?

I always got InvalidCastException.

code Employee:

C#
public Employee(string fName, string lName, int age, int earnings, DateTime dateOfRegistration)
		{
			this.First_Name = fName;
			this.Last_Name = lName;
			this.Age = age;
			this.Earnings = earnings;
			this.DateOfRegistration = dateOfRegistration;
		}


code Insert To Data Base: It work good!
C#
string command = string.Format("Insert into Employees Values ('{0}', '{1}', '{2}', '{3}', '{4}')",
				emp.First_Name, emp.Last_Name, emp.Age, emp.Earnings, emp.DateOfRegistration.Date);


code From data base to new employee: there is InvalidCastException:
C#
while(dr.Read())
			{
				emps.Add(new Employee((string)dr["First_Name"], (string)dr["Last_Name"], (int)dr["Age"], (int)dr["Earnings"],Convert.ToDateTime(dr["Date_of_registration"].ToString())));
			}
Posted
Comments
Mike Meinz 31-Aug-13 11:56am    
I don't think you have to do a conversion. I think you do have to tell C# that the value returned from the database using the dr class is a DateTime data type similar to what you did for the string and int data types.

Try this:


while(dr.Read())
{
emps.Add(new Employee((string)dr["First_Name"], (string)dr["Last_Name"], (int)dr["Age"], (int)dr["Earnings"],(DateTime)dr["Date_of_registration"]));
}
AjmLikeIt 31-Aug-13 11:58am    
Still that same error.
Mike Meinz 31-Aug-13 16:06pm    
Is there a possibility that the value in the database is NULL?

If it is possible that the value is NULL, than an ISNULL() in the SELECT statement or a dr.IsDBNull() check in the application should solve it.

Use Visual Studio Interactive Debugger to step through the code and see what is returned by dr["Date_of_registration"] or use SQL Server management Studio to execute the Select statement and see if any of the date values are returned as NULL.

1 solution

Assuming that the data in your DB is stored as a DateTime column, then it should just be a case of casting it:
C#
emps.Add(new Employee((string)dr["First_Name"], (string)dr["Last_Name"], (int)dr["Age"], (int)dr["Earnings"],(DateTime)dr["Date_of_registration"]));
If it isn't then you need to look at what is stored in your DB, as there is a major problem!
 
Share this answer
 
Comments
AjmLikeIt 31-Aug-13 12:10pm    
That doesn't help. I trying using "date", "datetime" and still that same error.
CHill60 31-Aug-13 12:14pm    
What is actually in dr["Date_of_registration"]
AjmLikeIt 31-Aug-13 12:17pm    
2013-08-31 00:00:00
CHill60 31-Aug-13 12:21pm    
That looks fine, are your regional settings such that dates are expected in yyyy-MM-dd format and not yyyy-dd-mm and is your database column a DateTime?
AjmLikeIt 31-Aug-13 12:27pm    
Where i can see these regional settings?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900