I am having a problem in fetching data into variable in c# from SQL Server.
Here is my code.
SqlConnection myCon = new SqlConnection(@"Data Source=BROWN-PC\KAZIM;Initial Catalog=Agency;Integrated Security=True");
string lastlogt;
string lastlogd;
myCon.Open();
SqlCommand cd2 = new SqlCommand(" SELECT TOP 1 * From (select Top 2 * from Agency.dbo.lastlogindt ORDER BY uid DESC) x ORDER BY uid",myCon);
SqlDataReader sdr;
sdr = cd2.ExecuteReader();
while (sdr.Read()) {
lastlogd = sdr.GetString(2);
lastlogt = sdr.GetString(3);
}
myCon.Close();
Dates.Text = lastlogd;
timing.Text = lastlogt;
Now the problem is that the columns of SQL Server from which I'm taking data is of date and time datatypes. and the error I am facing is
Unable to cast object of type 'System.DateTime' to type 'System.String'.