I am using dateTimePicker to collect date from user in a windows form to insert in SQL Server Database but when i debug it it says “connot convert dateTime into string” here is the code
string Agent = FieldAgentCombo.Text;
string Query = "INSERT INTO Comittment(Date,Field_Staff_Date,Detail,Priority,company_name,Name) values('" + Client + "','" + Agent + "','" + Date + "','" + FieldStaffDate + "','" + Detail + "','" + Priority + "')";
SqlCommand cmd = new SqlCommand(Query, conn);
int status = cmd.ExecuteNonQuery();
if (status > 0)
MessageBox.Show("record inserted");
Your code is vulnerable to SQL injection. I would recommend you using parametrized queries. Also in your SQL query you seem to have mixed the parameters. Make sure they are matching. For example:
This way the query is no longer vulnerable to SQL injection and in addition to that ADO.NET will take care of properly formatting the .NET types into the corresponding SQL types so that you don’t need to be doing any string parsing and date manipulations.