title: How to compare system date with date existing in database table
body: <h2>I have 2 columns in my table TimeTable</h2>
  
  <ol>
  <li><code>start_date</code> (format is <code>DD-MM-YYYY</code>)</li>
  <li><code>end_date</code></li>
  </ol>
  
  <p>If I log in from my login page, I want to check that current date is between <code>start_date</code> and <code>end_date</code>; if so, I am able to log in.</p>
  
  <p>How to do comparison in C#..asp.net..?</p>
  
  <p>I have printed current date in login page successfully.</p>
  
  <pre><code>lblMsg.Text= DateTime.Now.ToString("dd-MM-yyyy");  
  </code></pre>
  
  <p>I am using asp.net with c#</p>
  
  <pre><code>      start_date              end_date
  8/2/2013 12:00:00 AM    10/2/2013 12:00:00 AM
  </code></pre>
  
  <h2>Code for Storing Date in DD/MM/YYYY format</h2>
  
  <p>CultureInfo provider = CultureInfo.InvariantCulture;</p>
  
  <pre><code>        DateTime startdate = DateTime.ParseExact(tbstartdate.Text.ToString().Trim(), "dd/MM/yyyy", provider);
          DateTime enddate = DateTime.ParseExact(tbenddate.Text.ToString().Trim(),"dd/MM/yyyy",provider);
          string sql = "INSERT INTO TimeTable(start_date,end_date)"
       + "VALUES (@startdate,@enddate)";
          SqlConnection conn = new SqlConnection(ConnectionString);
          conn.Open();
          SqlCommand cmd = new SqlCommand(sql, conn);
  
          cmd.Parameters.Add("@startdate", SqlDbType.DateTime).Value = startdate;
          cmd.Parameters.Add("@enddate", SqlDbType.DateTime).Value = enddate;
  
          cmd.Prepare();
          n = cmd.ExecuteNonQuery();
  
          conn.Close();
  </code></pre>
  
  <h2>Code for Checking Date on login page</h2>
  
  <p>CultureInfo provider = CultureInfo.InvariantCulture;</p>
  
  <pre><code>        DateTime date = DateTime.ParseExact(DateTime.Now.ToString("dd/mm/yyyy"), "dd/MM/yyyy", provider);
        //  DateTime date=DateTime.Now.ToString("dd/mm/yyyy");
  
          string query1 = "select * from TimeTable where ((start_date &gt;= @date1 and End_date&lt;= @date2) and UserType=@type)";
          SqlConnection conn = new SqlConnection(ConnectionString);
          conn.Open();
          SqlCommand com = new SqlCommand(query1, conn);
          com.Parameters.Add("@type",dpusertype.SelectedItem.Text.ToString());
          com.Parameters.Add("@date1",date);
          com.Parameters.Add("@date2", date);
          com.CommandType = CommandType.Text;
          SqlDataAdapter da = new SqlDataAdapter(com);
          DataTable dt = new DataTable();
          da.Fill(dt);
  </code></pre>
  
  <p>`if (dt.Rows.Count > 0)  // comparing users from table </p>
  
  <pre><code>        {
  
              FormsAuthentication.RedirectFromLoginPage(txtFcode.Text, Persist.Checked);
              Response.Redirect("~/DeptCoordinator/DeptCoordinator_homepage.aspx");  
          }
  </code></pre>
  
  <h2>Getting Error</h2>
  
  <p>The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar.
  Desc`</p>