Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
I have stored a timespan value in sqlserver database in Time(7) format but now i need to retieve that timespan value and perform some calculation i used this code but its showing error


C#
string tm1 = "Select Test1Time from Result where UserName='" + ref1.ToString() + "'";
sqlCommand tkk1 = new SqlCommand(tm1, con);
TimeSpan tm11 = tkk1.ExecuteScalar;
SqlCommand tkk2 = new SqlCommand(tm2, con);

and
C#
TimeSpan tm33 = TimeSpan.Parse(tkk3.ToString());



[edit]code blocks added[/edit]
Posted
Updated 17-Mar-13 0:03am
v2
Comments
Michael Haephrati 16-Mar-13 16:41pm    
Another advice: prepare a small code sample of your problem (place there only what is necessary). Submit here this code sample along with the exact error messages you receive (even a screenshot).

1 solution

There are several mistakes you are doing.
TimeSpan is used to calculate the difference between two dates / times. You SQL query returns a single value - Test1Time.
This is probably either a string or a single date/time. So you don't need to use TimeSpan, and you normally won't store a Time Span in your database. Instead you would store a single date/time and use Time Span for example, to calculate the difference between the value you have saved in the database and the current date/time.

Assuming you have fixed that, First, to convert DateTime into TimeSpan use this:
C#
TimeSpan span1 = new TimeSpan(tm1.Now.Ticks),span2 = new TimeSpan(tm2.Now.Ticks),span3 = new TimeSpan(tm3.Now.Ticks);

To add them three, you need :
C#
TimeSpan span1=tm11;
TimeSpan total = span1.Add(span2);
total=total.Add(span3);
 
Share this answer
 
v2
Comments
Member 8780842 16-Mar-13 16:36pm    
How to modify the sqlquery to get in time value and then add that please
Michael Haephrati 16-Mar-13 16:39pm    
Change this line
TimeSpan tm11 = tkk1.ExecuteScalar;
into
DateTime tm11 = tkk1.ExecuteScalar;

Read here about TimeSpan and DateTime and the difference between them
http://msdn.microsoft.com/en-us/library/system.timespan.aspx
Member 8780842 16-Mar-13 16:43pm    
Then also it is showing error in executescalar
Michael Haephrati 16-Mar-13 16:45pm    
So it is saved as a string. Read the query result into a string argument and only then convert it or use it as is. Again, it would be best if you send full source code and exact error message
Member 8780842 16-Mar-13 16:49pm    
string tm1 = "Select Test1Time from Result where UserName='" + ref1.ToString() + "'";
string tm2 = "Select Test2Time from Result where UserName='" + ref1.ToString() + "'";
string tm3 = "Select Test3Time from Result where UserName='" + ref1.ToString() + "'";
SqlCommand tkk1 = new SqlCommand(tm1, con);
DateTime tm11 = DateTime.Parse(tkk1.ExecuteScalar);
SqlCommand tkk2 = new SqlCommand(tm2, con);
TimeSpan tm22 = TimeSpan.Parse(tkk2.ToString());
SqlCommand tkk3 = new SqlCommand(tm3, con);
TimeSpan tm33 = TimeSpan.Parse(tkk3.ToString());
TimeSpan totaltime = (tm11.Add(tm22.Add(tm33)));

string insCmd13 = "Update Result Set Totaltime=@Test3Mark,totalmark=@Test3Time Where UserName=@UserName ";

SqlCommand insertUser13 = new SqlCommand(insCmd13, con);
insertUser13.Parameters.AddWithValue("@UserName", ref1.ToString());
insertUser13.Parameters.AddWithValue("@Test3Mark", totalmark.ToString());
insertUser13.Parameters.AddWithValue("@Test3Time", totaltime);

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