Click here to Skip to main content
15,868,340 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
this code runs with out any error,but it stores the time value as "1/1/1900 9:34:00 AM" in the table.How can we get only the time out of this? Here the date is also wrong.Why is it happening like that?
please help me!!!

openConnection();
string query = "insert into votes ([time],[V1],[V2],[V3]) values (@time,@V1,@V2,@V3)";
SqlCommand cmd2 = new SqlCommand(query, con);

cmd2.Parameters.Add(new SqlParameter("@time", DateTime.Now.ToShortTimeString()));
cmd2.Parameters.Add(new SqlParameter("@V1", selCand_No1));
cmd2.Parameters.Add(new SqlParameter("@V2", selCand_No2));
cmd2.Parameters.Add(new SqlParameter("@V3", selCand_No3));

cmd2.ExecuteNonQuery();
MessageBox.Show("Succesfully Inserted");
closeConnection();
Posted
Updated 3-Jul-11 18:51pm
v2

The problem probably comes down to the type of object you're storing as 'time'. What sort of object is it, in your database ? I suspect that the DB can't work out what to do with the value you're passing. Of course, it's clear from the result, that you're using an object that expects a date/time and just passing in a time. Is the time portion correct ? If so, if you're using a DateTime type object in the DB, it is doing the best it can, when you're failing to provide the date portion.
 
Share this answer
 
Comments
Member 7779792 4-Jul-11 1:18am    
thnx a lot!
as you said i have created the [time] field as DateTime.now i changed it in to varchar, now it runs as i expected.
thank you very much. :))))
The probable reason could be the datatype of the column, is it DateTime? If yes then how you can enter time value in datetime type column (design problem).
 
Share this answer
 
Try this it returns you only time from whole format
mydate is my datetime column in the table employee.


select convert(nvarchar,mydate,8)  from employee

thanks
 
Share this answer
 

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