sathish kumar

sathish kumar

  • NA
  • 117
  • 165.2k

Date fromat in sql server2008 : String was not recognized as a valid DateTime.

Aug 2 2012 1:22 AM
hi friends.

  I m using a date picked in dd/MM/yyyy format.

but for expiry date i want only in MM/YYYY format. so i enabled auto postback property of txtExpiryDate and written event as below. it comes like MM/YYYY format.But while saving i m gettting error as String was not recognized as a valid DateTime.

         protected void txtExpiryDate_TextChanged(object sender, EventArgs e)
    {
      

        string dbConn = ConfigurationManager.ConnectionStrings["CMC"].ConnectionString;
        SqlConnection sqlConn = new SqlConnection(dbConn);
        sqlConn.Open();
        string strSQL;

        strSQL = "Select RIGHT(CONVERT(VARCHAR(10),ExpiryDate, 105), 7) AS [ExpiryDate] from PurchaseBillDetails";
        SqlCommand cmdTestHeads = new SqlCommand(strSQL, sqlConn);
        SqlDataReader drTestHeads = cmdTestHeads.ExecuteReader();
        if (drTestHeads.Read())
        {
            txtExpiryDate.Text = drTestHeads[0].ToString();
        }
        sqlConn.Close();
    }



while saving i m using like



   DateTime ExDate = DateTime.ParseExact(txtExpiryDate.Text, "dd-MM-yyyy", CultureInfo.InvariantCulture);

                  //  string ExDate = Convert.ToDateTime(txtExpiryDate.Text).ToString();
                    DateTime PbillDate = DateTime.ParseExact(txtPbDate.Text, "dd-MM-yyyy", CultureInfo.InvariantCulture);

                    SqlConnection sqlConn1 = new SqlConnection(dbConn);
                    sqlConn1.Open();

                    string strSQL1 = "INSERT INTO [Temp_PurchaseBill]([ITEMCODE],[ITEMDESCRIPTION],[BatchNo],[ExpiryDate],[QTY],[PackQTY],[FreeQTY],[StockQTY],[TaxType],[PURCHASERATE],[MRP],[TaxScheme],[ITEMRATE],[MRPRATE],[DiscPer],[DiscTotal],[VATOn],[VatPercentage],[VatTotal],[FreeVatAmt],[MrpAbate],[TotalAmount],[BillNO],[BillDate],[VatOptions])VALUES('" + strItem + "','" + ddlDescription.SelectedItem.ToString() + "','" + txtBatchNo.Text + "','" + ExDate + "','" + txtQty.Text + "','" + txtQtyPack.Text + "','" + txtFree.Text + "','" + txtStockQty.Text + "','" + ddlTaxType.SelectedItem.ToString() + "','" + txtRate.Text + "','" + txtMrp.Text + "','" + ddlTaxScheme.SelectedItem.ToString() + "','" + txtPurchaseValue.Text + "','" + txtMrpValue.Text + "','" + txtDiscountPercent.Text + "','" + txtDiscountAmt.Text + "','" + ddlVatType.SelectedItem.ToString() + "','" + ddlVatpercent.SelectedItem.ToString() + "','" + txtVat.Text + "','" + txtTaxOnFreevat.Text + "','" + txtMrpAbate.Text + "','" + txtTotalValue.Text + "','" + txtPbNo.Text + "','" + PbillDate + "','" + ddlDiscountOption .SelectedItem.ToString()+ "')";
                    SqlCommand cmd1 = new SqlCommand(strSQL1, sqlConn1);
                    cmd1.ExecuteNonQuery();

i tried even with DateTime ExDate = DateTime.ParseExact(txtExpiryDate.Text, "MM-YYYY", CultureInfo.InvariantCulture);

but it s not working...

Help me please...Its urgent....


Thanks in advance.....


Answers (1)