Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
i just want to pass date and time to a database column. but it only delivers date part of the Datetime variable.Does anyone have any suggestions.

C#
String procedureName;
CommandType cmdType;
List<sqlparameter> parameters = new List<sqlparameter>();

//setting procedure properties
procedureName = "EX_Updater";
cmdType = CommandType.StoredProcedure;

parameters.Add(new SqlParameter("@curCode", SqlDbType.VarChar, 10));
parameters.Add(new SqlParameter("@curExRate", SqlDbType.VarChar, 10));
parameters.Add(new SqlParameter("@updatedOn", SqlDbType.VarChar, 10));
parameters.Add(new SqlParameter("@updatedBy", SqlDbType.VarChar, 10));

string date = DateTime.Now.ToString();

//Inserting data through stored procedure
parameters[0].Value = CountryCode.Replace("'", "''");
parameters[1].Value = ExchangeRate.Replace("'", "''");
parameters[2].Value = DateTime.Now;
parameters[3].Value = U_id;


//pass to common procedure
Boolean status = SQLTrans.executeProcedure(procedureName, cmdType, parameters);
//This will only pass "DATE" part value to the database and not passing Time value to the database. 

In database updatedOn is declared as "datetime ".i can assure that parameter will be getting date and time values.but whenit comes to DB it only stores datepart. i also tried by giving a manual string("4/20/2012 2:51:52 PM")to that parameter value.but it also did not saved in the database.But i can manually insert records to the DB.
Posted
Updated 25-Apr-12 18:23pm
v4
Comments
VJ Reddy 26-Apr-12 0:23am    
Edit: pre tag for C# code added.
VJ Reddy 26-Apr-12 0:24am    
Thank you for accepting the solution.

The reason could be that the updatedOn parameter is defined as
C#
parameters.Add(new SqlParameter("@updatedOn", SqlDbType.VarChar, 10));

where the length is 10. But the Date with Time together is more than 10 chars long
Note: This could be a possibility, I have not verified.

Secondly, the SqlDbType is taken as VarChar whereas DateTime may be more appropriate as given here
http://msdn.microsoft.com/en-us/library/system.data.sqldbtype.aspx[^].

Further, the SqlParameterCollection.AddWithValue Method is better instead of adding a parameter and then assigning a value, as explained here
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.addwithvalue.aspx[^]
 
Share this answer
 
Comments
Hesha 26-Apr-12 0:23am    
Thanks a lot Mate..it worked!!! parameter datatype was the problem!!! Thanks for explanation!!!
VJ Reddy 26-Apr-12 0:25am    
You're welcome.
Thank you for the quick response.
increase the size of @updatedOn parameter from 10 to 20.
 
Share this answer
 
Comments
member60 26-Apr-12 4:36am    
My 5!

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