SqlException unhandled : Incorrect syntax near '12' - Programmers Heaven

Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

SqlException unhandled : Incorrect syntax near '12'

rf_librarf_libra Posts: 5Member
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

Sign In or Register to comment.