Azmodan
Azmodan

Reputation: 107

Read Date from SQL Server

How do I read a date from SQL Server to a datetime variable in C# ?

DateTime sDate = (read["notDate"]);

I get error

Cannot convert object to a system.datetime [are you missing a cast?]

Edit: the SQL Server uses a Date datatype for this column.

Then how will format the sDate to UK format - I was looking at

format("dd/MM/yyyy")

Using ASP.NET, C#, SQL Server Express.

Thanks for your help.

Upvotes: 1

Views: 4154

Answers (1)

Josh Stevens
Josh Stevens

Reputation: 4221

you need to cast it into a date.. this should do the job

DateTime sDate = (DateTime)read["notDate"];

To then format it into a dd/MM/yyyy you can do

string formattedDate = sDate.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);

Upvotes: 4

Related Questions