0
string str,a;
DateTime dat, aa;

using (con = new SqlConnection(@"Data Source=daniyal\sqlexpress;Initial Catalog=webassign;Integrated Security=True"))
{
    con.Open();

    using(com = new SqlCommand("select * from tasks",con))
    {
        rdr = com.ExecuteReader();

        while (rdr.Read())
        {
            str = rdr.GetValue(1).ToString();
            dat = DateTime.ParseExact(str, "dd/MM/yyyy", CultureInfo.InvariantCulture);
        }
    }
}

I want to parse a varchar variable into datetime saved in my SQL Server

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0

It doesn't work because the month is indicated with one digit but in the format string there are two MM. If you want to use ParseExact "the format of the string representation must match the specified format exactly." https://msdn.microsoft.com/en-us/library/w2sa9yss%28v=vs.110%29.aspx

If you can't change the way you save the date in the database you need to find another strategy.

Paolo Costa
  • 1,989
  • 1
  • 12
  • 15