Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys I'm trying to read data type TIME but it throws this exeption:
Unable to cast object of type System.TimeSpan to type System.String.

Here is my code:

C#
SqlCommand novyprikaz = new SqlCommand("SELECT * FROM stanice WHERE id=" + selectrow, spojeni);
             spojeni.Open();
             SqlDataReader precti = novyprikaz.ExecuteReader();

             if (precti.Read())
             {
                 
                 textBox2.Text = precti.GetString(precti.GetOrdinal("odjezd"));
}


How should I improve my code to read it this data type TIME ?
column "odjezd" is in data type TIME
Thanks in advance
Posted
Updated 15-Jul-13 0:48am
v2
Comments
Maciej Los 15-Jul-13 6:50am    
What version of MS SQL Server?
mareksip 15-Jul-13 6:59am    
2012

string time = Convert.ToDateTime(row["PO_Date"].ToString()).ToString("dd-MMM-yyyy");
 
Share this answer
 
Try:

C#
string time = new DateTime(precti.GetOrdinal("odjezd").Ticks).ToString("HH:mm:ss");


See if this works.
 
Share this answer
 
Comments
mareksip 15-Jul-13 7:05am    
It might work, but how do I get it into textbox2?
Priyanka7777 15-Jul-13 7:10am    
textBox2.Text = new DateTime(precti.GetOrdinal("odjezd").Ticks).ToString("HH:mm:ss");

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