datetime type field insert
-
i want insert a recored in sql that one field is datetime type, i write this code but it has error , do u know why? string insertCommand = insert into Paper_Ajance (Paper_ID,Request,Paper_Date) values ('111','True',@MyParam); using (SqlConnection cnn = new SqlConnection(ConnectionString)) { using (SqlCommand cmm = new SqlCommand()) { cnn.Open(); cmm.Connection = cnn; cmm.CommandText = insertCommand; SqlParameter param = new SqlParameter(); param.ParameterName = "@MyParam"; param.Value = DateTime.Now; cmm.Parameters.Add(param); cmm.ExecuteNonQuery(); } }
-
i want insert a recored in sql that one field is datetime type, i write this code but it has error , do u know why? string insertCommand = insert into Paper_Ajance (Paper_ID,Request,Paper_Date) values ('111','True',@MyParam); using (SqlConnection cnn = new SqlConnection(ConnectionString)) { using (SqlCommand cmm = new SqlCommand()) { cnn.Open(); cmm.Connection = cnn; cmm.CommandText = insertCommand; SqlParameter param = new SqlParameter(); param.ParameterName = "@MyParam"; param.Value = DateTime.Now; cmm.Parameters.Add(param); cmm.ExecuteNonQuery(); } }
ptvce wrote:
but it has error
Don't you think telling us what the error is would be helpful? Also, please format your code snippets appropriately. Read the forum guidelines. If you following the guidelines you are more likely to receive help.
I know the language. I've read a book. - _Madmatt
-
i want insert a recored in sql that one field is datetime type, i write this code but it has error , do u know why? string insertCommand = insert into Paper_Ajance (Paper_ID,Request,Paper_Date) values ('111','True',@MyParam); using (SqlConnection cnn = new SqlConnection(ConnectionString)) { using (SqlCommand cmm = new SqlCommand()) { cnn.Open(); cmm.Connection = cnn; cmm.CommandText = insertCommand; SqlParameter param = new SqlParameter(); param.ParameterName = "@MyParam"; param.Value = DateTime.Now; cmm.Parameters.Add(param); cmm.ExecuteNonQuery(); } }
ptvce wrote:
string insertCommand = insert into Paper_Ajance (Paper_ID,Request,Paper_Date) values ('111','True',@MyParam);
For now, the first error in your code is - you have not put the insertCommand string in double quotes! If you read forum guidelines properly, posting your code/question correctly would get you expected response. Update your question, format it correctly, post error if you face it after using 'double quotes' around insert command string. One more thing, do try to define the Parameter type as DateTime (assuming database too has a defined Datetime field)