0

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)
    {
    }
}
2
  • what's data type of obj.Payment_date? Commented Oct 25, 2014 at 11:27
  • The payment_date column must be of type DATETIME in the database. The obj.Payment_date should be a DateTime type, not a string. Commented Oct 25, 2014 at 11:40

2 Answers 2

2

Make Sure you have created Payment_date with DateTime Data type.

and set Date & Time like this:

obj.Payment_date= DateTime.Now
Sign up to request clarification or add additional context in comments.

Comments

1

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);

2 Comments

Great, Thanks Giorgos Betsos
The data type of these parameters are not specified which is causing the issue I think. It's always a good practice to specify the data type values in SQL parameters :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.