Click here to Skip to main content
15,887,854 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
I am saving values to a database but I am not able to save them with current Time.

I have created a colume with name Time in database, but dont know how to save time.


C#
SqlConnection sqlconn  = new SqlConnection("Data Source=AAN-PC;Initial Catalog=latestsensordata;Integrated Security=True");
       SqlDataAdapter sa = new SqlDataAdapter();
       string query = "insert into Table_1(SensorValue,Time) values(" +hexValue+")";
                        sa.InsertCommand = new SqlCommand(query, sqlconn);
Posted
Updated 16-Jan-13 23:32pm
v4
Comments
The Doer 17-Jan-13 5:02am    
What is "hexvalue" here??
You are passing only one parameter to your INSERT statement while mentioning it as two.It astounds .. !
Amir Mahfoozi 21-Jan-13 1:04am    
Hi,
why don't you define a default value of "getdate()" for the time field at the database level ?
You can ignore this field when inserting new data to the table. It automatically gets server time of insertion.

Hi,

For consistency I would assume you require the server time and not the client pc time.

All you need to do is set the Time on your table with a default of Getdate().
 
Share this answer
 
v3
HI,

Here you are passing only session values.

string query = "insert into Table_1(SensorValue,Time) values(" +hexValue+ "," +DateTime.Now.ToString("HH:MM:ss")+")";


Thanks
 
Share this answer
 
v2
Comments
ontheline89 17-Jan-13 5:08am    
I am getting error in above your statement.
Invalid Expresstion ","
[no name] 17-Jan-13 6:51am    
Updated the solution dear please check.
fjdiewornncalwe 17-Jan-13 9:59am    
Paramatarized Queries are the way to go. We should NEVER suggest inline values like this. They are nothing but an invitation to SQL injection.
[no name] 17-Jan-13 10:01am    
Thanks for the info dear...
Hi,

For consistency I would assume you require the server time and not the client pc time.

All you need to do is set the Time on your table with a default of Getdate().

In this way you don't pass any value on insert.
 
Share this answer
 
Pass the value datetime.now() value to time. Then it inserts a value along with current time.
 
Share this answer
 
i think this will help You..
SqlConnection sqlconn = new SqlConnection("Data Source=AAN-PC;Initial Catalog=latestsensordata;Integrated Security=True");
                      SqlDataAdapter sa = new SqlDataAdapter();
                      string CurrntTime = System.DateTime.Now.TimeOfDay.ToString();
                      string query = "insert into Table_1(SensorValue,Time) values(" + hexValue + ",CurrntTime)";
                      sa.InsertCommand = new SqlCommand(query, sqlconn);
 
Share this answer
 
v2
Comments
ontheline89 17-Jan-13 5:23am    
I am getting error,
Invalid column name CurrntTime.

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