SqlException unhandled : Incorrect syntax near '12'

Dear users,

I am having problem with inserting date into SQL server. My code is as following:

[code] protected void btnSave_Click(object sender, EventArgs e)

{
using (SqlConnection con = new SqlConnection("Data Source=MEHDI-PC\SQLEX;Initial Catalog=PIMS;Integrated Security=True"))
{
using (SqlCommand cmd = new SqlCommand())
{

string DatePrinted = "";

if (txtDatePrinted.Text != "")
DatePrinted = DateTime.ParseExact(txtDateReceived.Text, "G", CultureInfo.GetCultureInfo("en-US")).ToString("yyyy-MM-dd");
else
DatePrinted = "NULL";

string DateReceived = "";

if (txtDateReceived.Text != "")
DateReceived = DateTime.ParseExact(txtDateReceived.Text, "G", CultureInfo.GetCultureInfo("en-US")).ToString("yyyy-MM-dd");
else
DateReceived = "NULL";

string DueDate = "";

if (txtDueDate.Text != "")
DueDate = DateTime.ParseExact(txtDateReceived.Text, "G", CultureInfo.GetCultureInfo("en-US")).ToString("yyyy-MM-dd");
else
DueDate = "NULL";

string ActualDate = "";

if (txtActualDate.Text != "")
ActualDate = DateTime.ParseExact(txtDateReceived.Text, "G", CultureInfo.GetCultureInfo("en-US")).ToString("yyyy-MM-dd");
else
ActualDate = "NULL";

String sql = "Insert into dbo.Documents(Ref,Subject,Src,Dst,Medium,Date_Printed,Date_Received,Document_Type,Action_Required,Due_Date,Actual_Date,[Content],Tag,Issue_No,Attachment,Notes,Assigned_To,Reply_Ref,Priority,Status,Response) values ('" + txtRef.Text + "','" + txtSubject.Text + "','" + ddlSource.Text + "' ,'" + ddlDestination.Text + "','" + ddlMedium.Text + "','" + DatePrinted + "','" + DateReceived + "','" + ddlDocumentType.Text + "','" + cbxAction.Checked + "','" + DueDate + "','" + ActualDate + "','" + txtContent.Text + "','" + txtTag.Text + "','" + txtIssue.Text + "','" + txtAttachment.Text + "','" + txtNotes.Text + "','" + ddlAssignedTo.Text + "','" + txtReplyRef.Text + "','" + ddlPriority.Text + "','" + ddlStatus.Text + "','" + ddlResponse.Text + "')";

cmd.Connection = con;
cmd.CommandText = sql;

con.Open();
//dataset object to get all select statement results
//DataSet ds = new DataSet();

//sql dataadoptor to fill dataset
cmd.ExecuteNonQuery();
}
if (con.State == ConnectionState.Open)
{
con.Close();
}

MultiView1.SetActiveView(vRecord);
txtRef.Text = string.Empty;
txtSubject.Text = string.Empty;
ddlSource.Text = string.Empty;
ddlDestination.Text = string.Empty;
ddlMedium.Text = string.Empty;
txtDatePrinted.Text = string.Empty;
txtDateReceived.Text = string.Empty;
ddlDocumentType.Text = string.Empty;
cbxAction.Checked = false;
txtDueDate.Text = string.Empty;
txtActualDate.Text = string.Empty;
txtContent.Text = string.Empty;
txtTag.Text = string.Empty;
txtIssue.Text = string.Empty;
txtAttachment.Text = string.Empty;
txtNotes.Text = string.Empty;
ddlAssignedTo.Text = string.Empty;
txtReplyRef.Text = string.Empty;
ddlPriority.Text = string.Empty;
ddlStatus.Text = string.Empty;
ddlResponse.Text = string.Empty;
}

}[/code]


After I run this code, and enter any date value in the datefield, it throws this message:

SqlException unhandled : Incorrect syntax near '12'


I am failed to see anything wrong with my code. Please help me identify the problem.

You help will be much appreciated.

Regards

Comments

  • please check record variable name in database and queries, your logic is good try to review variable.
  • please check record variable name in database and queries, your logic is good try to review variable.
  • please check record variable name in database and queries, your logic is good try to review variable.
  • I have got it fixed somehow. Thanks alot for your reply.

  • I have got it fixed somehow. Thanks alot for your reply.

  • I have got it fixed somehow. Thanks alot for your reply.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories