1
Answer

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

Photo of sathish kumar

sathish kumar

12y
1.4k
1
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)

0
Photo of Datta Kharad
NA 2.5k 40.9k 13y
Hi
   Declare array outside the for loop and get another variable total for sum...
You can display each number which is meet your condition i,e multiple of 3 or 5 and Lastly display Sum of these numbers. Use this code:-
using System;


namespace test
{
class Program
{
static void Main()
{
Program p = new Program();
p.sum(1000);


Console.ReadLine();
}


public void sum (int tal)
{
long total = 0;
int[] array= new int[1000];  //It does save in Array..(Here your problem get solved)
for (int i = 1; i < tal; i++)
{
if (i % 3 == 0 || i % 5 == 0)
{
array[i] += i;
 total = total + i;
Console.WriteLine(array[i]);
}
}
Console.WriteLine("Sum of multiple 3 or 5 below 1000= "+total);
}
}
}
Accepted
0
Photo of Datta Kharad
NA 2.5k 40.9k 13y
Hi
   If your query resolved then mark as Correct Answer.
0
Photo of Zu Sung Park
NA 5 2.4k 13y
Thank you so much! :) I used this tips since I wanted to use Array.
0
Photo of Zu Sung Park
NA 5 2.4k 13y
Thank you so much! :) Good tips!
0
Photo of Armando Pinto
NA 2 0 13y
Hi.

Looking at your code I'm not sure of what you want to do... 

If you want to get the total sum, you should maybe do something like this:

namespace test
{
  class Program
  {
        static void Main(string[] args)
        {
                long totalSum = Sum(1000);
                Console.WriteLine(string.Format("Total sum: {0}", totalSum));
                Console.ReadLine();
        }

        public static long Sum(int tal)
        {
                long total = 0;
                for (int i = 1; i < tal; i++)
                {
                        if (i % 3 == 0 || i % 5 == 0)
                        {
                                total += i;
                        }
                }

                return total;
        }
   }
}