Link to home
Start Free TrialLog in
Avatar of sana khan
sana khanFlag for India

asked on

Showing errors of date as "string was not recognized as a valid datetime"

 public void databind2()
    {
        SqlCommand Comm1 = new SqlCommand("select FORMAT(deadlineDate,'dd-MM-yyyy') from FormDetails where FormId=" + FormIDs.Client_BulkPostCard + " and Edition_Id='" + Session["Edition_ID"].ToString() + "'", con);
        con.Open();
        SqlDataReader dr = Comm1.ExecuteReader();
        if (dr.Read())
        {
            Label1.Text = dr[0].ToString();          
        }

        DateTime dt1 = DateTime.Parse(Label1.Text);
        DateTime dt2 = DateTime.Parse(DateTime.Now.ToString());

        int days = (dt1 - dt2).Days;

        if (days > 0)
        {
            lblNoOfDays.Text = days + " DAYS LEFT";
        }
        else
        {
            lblNoOfDays.Text = "DEADLINE PASSED";
        }
        con.Close();
    }

Open in new window


The dt1 date is coming from database in the format as dd-mm-yyyy(31-12-17) but the current date which is stored in dt2 is coming from system as dd-mmm-yyyy(01-nov-17)
so it showing error . Please help me solving this issue using Asp.Net
Avatar of Kimputer
Kimputer

If you are really really sure the input is always the same format, try this:

https://msdn.microsoft.com/en-us/library/w2sa9yss(v=vs.110).aspx
One of your date's cannot be parsed.  Might want to consider using ParseExact; e.g. -
using System;
using System.Globalization;

namespace EE_Q29065846
{
    class Program
    {
        static void Main(string[] args)
        {
            string date1 = "31-12-17", date2 = "01-nov-17";
            DateTime dt1 = DateTime.ParseExact(date1, new string[] { "dd-MM-yy", "dd-MM-yyyy" }, CultureInfo.CurrentCulture, DateTimeStyles.None);
            DateTime dt2 = DateTime.ParseExact(date2, new string[] { "dd-MMM-yy", "dd-MMM-yyyy", "dd-MMMM-yy", "dd-MMMM-yyyy" }, CultureInfo.CurrentCulture, DateTimeStyles.None);

            int days = (dt1 - dt2).Days;
            if (days > 0)
                Console.WriteLine("{0} DAYS LEFT", days);
            else
                Console.WriteLine("DEADLINE PASSED");
            Console.ReadLine();
        }
    }
}

Open in new window


-saige-
Why perform this line?

DateTime dt2 = DateTime.Parse(DateTime.Now.ToString());

DateTime.Now is a DateTime value which you could then use in your calculation of days:

int days = (dt1 - DateTime.Now).Days;
Avatar of sana khan

ASKER

I am sure of database date that is in variable dt1 but i am not sure about the system date.
If you need to assign "today" to dt2, you can just do:

DateTime dt2 = DateTime.Today;
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Error is giving on this line:

DateTime dt1 = DateTime.Parse(Label1.Text);

Open in new window

As I stated, your date (or dates) cannot be parsed...  Use ParseExact.

Proof of concept -
using System;
using System.Globalization;

namespace EE_Q29065846
{
    class Program
    {
        static void Main(string[] args)
        {
            string date1 = "31-12-17", date2 = "01-nov-17";
            ParseDates(date1, date2);
            ParseExactDates(date1, date2);
            Console.ReadLine();
        }

        static void ParseDates(string date1, string date2)
        {
            try
            {
                DateTime dt1 = DateTime.Parse(date1);
                Console.WriteLine("Date1 parsed - {0}", dt1);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error parsing date1 - {0}", ex.Message);
            }

            try
            {
                DateTime dt2 = DateTime.Parse(date2);
                Console.WriteLine("Date2 parsed - {0}", dt2);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error parsing date2 - {0}", ex.Message);
            }
        }

        static void ParseExactDates(string date1, string date2)
        {
            try
            {
                DateTime dt1 = DateTime.ParseExact(date1, new string[] { "dd-MM-yy", "dd-MM-yyyy" }, CultureInfo.CurrentCulture, DateTimeStyles.None);
                Console.WriteLine("Date1 parsed - {0}", dt1);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error parsing date1 - {0}", ex.Message);
            }

            try
            {
                DateTime dt2 = DateTime.ParseExact(date2, new string[] { "dd-MMM-yy", "dd-MMM-yyyy", "dd-MMMM-yy", "dd-MMMM-yyyy" }, CultureInfo.CurrentCulture, DateTimeStyles.None);
                Console.WriteLine("Date2 parsed - {0}", dt2);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error parsing date2 - {0}", ex.Message);
            }
        }
    }
}

Open in new window

Produces the following output -User generated image
-saige-
I got the solution using this code