I am creating a new application and I am trying to insert a DateTime data type into SQLServer. the error says
Conversion failed when converting date and/or time from character string.
Code:
string Insertcmd = "INSERT INTO PatientPay (PatientID, Cash, AmountPaid,
PaymentDate, Reseaon, StaffID) Values (@StaffID, @Cash, @AmountPaid,
@type, @PaymentDate, @StaffID)";
cmd = new SqlCommand(Insertcmd, con);
SqlParameter[] param = new SqlParameter[6];
param[0] = new SqlParameter("@PatientID", SqlDbType.Int);
param[0].Value = PatientID;
param[1] = new SqlParameter("@Cash", SqlDbType.Float);
param[1].Value = Cash;
param[2] = new SqlParameter("@AmountPaid", SqlDbType.Float);
param[2].Value = AmountPaid;
param[3] = new SqlParameter("@type", SqlDbType.NVarChar, 255);
param[3].Value = type;
param[4] = new SqlParameter("@PaymentDate", SqlDbType.DateTime);
param[4].Value = Convert.ToDateTime( DateTime.Now.ToString("yyyy-
MM-dd 00:00:00")); // Error
param[5] = new SqlParameter("@StaffID", SqlDbType.Int, 255);
param[5].Value = StaffID;
//Open the connection to database
con.Open();
try
{
if (con.State == System.Data.ConnectionState.Open)
{
//Execute the Adding process
cmd.Parameters.AddRange(param);
cmd.ExecuteNonQuery();
}
}catch{}