Dear users,
I am having problem with inserting date into SQL server. My code is as following:
You help will be much appreciated.
I am having problem with inserting date into SQL server. My code is as following:
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_Prin ted,Date_Received,Document_Type,Action_Required,Du e_Date,Actual_Date,[Content],Tag,Issue_No,Attachme nt,Notes,Assigned_To,Reply_Ref,Priority,Status,Res ponse) 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; } }
You help will be much appreciated.