Reputation: 411
I'm trying for two days to add date in my database table but every time I see this 12:00:00 AM
value instead of actual time. I tried enough for this simple issue.
http://puu.sh/cpP2l/0b0dd66042.png
obj.Payment_date = DateTime.Now.ToString("G");
using (SqlCommand command = new SqlCommand("INSERT into Payments([software_id] ,[email] ,[ip] ,[payment_status] ,[payment_date] ,[transaction_id] ,[amount] ,[license_type] ,[license_status] ,[cd_key]) VALUES(@software_id ,@email ,@ip ,@payment_status ,@payment_date ,@transaction_id ,@amount ,@license_type ,@license_status ,@cd_key)", con))
{
command.Parameters.Add(new SqlParameter("software_id", obj.Software_id));
command.Parameters.Add(new SqlParameter("email", obj.Email));
command.Parameters.Add(new SqlParameter("ip", obj.IP));
command.Parameters.Add(new SqlParameter("payment_status",obj.Payment_status));
command.Parameters.Add(new SqlParameter("payment_date", obj.Payment_date));
command.Parameters.Add(new SqlParameter("transaction_id", obj.Transaction_id));
command.Parameters.Add(new SqlParameter("amount", obj.Amount));
command.Parameters.Add(new SqlParameter("license_type", obj.License_type));
command.Parameters.Add(new SqlParameter("license_status",obj.License_status));
command.Parameters.Add(new SqlParameter("cd_key", obj.Cd_key));
if (command.ExecuteNonQuery() > 0)
{
}
}
Upvotes: 0
Views: 119
Reputation: 72175
If what you want is to record the date and time by the time of insertion then you'd better add a default constraint to your column:
ALTER TABLE Payments
ADD CONSTRAINT df_CurrentDateTime
DEFAULT CURRENT_TIMESTAMP FOR [Payment_Date]
In this case you do not need to provide a parameter for Payment_Date field at all.
EDIT: If the above solution is not suitable for your needs, then I would suggest providing the parameter type for the Payment_Date parameter used by your Command Object.
e.g. try sth like:
SqlParameter myParam = new SqlParameter("payment_date", obj.Payment_date);
myParam.DbType = System.Data.DbType.DateTime;
command.Parameters.Add(myParam);
Upvotes: 1
Reputation: 631
Make Sure you have created Payment_date with DateTime
Data type.
and set Date & Time like this:
obj.Payment_date= DateTime.Now
Upvotes: 2