0

I have 2 columns in my table TimeTable

  1. start_date (format is DD-MM-YYYY)
  2. end_date

If I log in from my login page, I want to check that current date is between start_date and end_date; if so, I am able to log in.

How to do comparison in C#..asp.net..?

I have printed current date in login page successfully.

lblMsg.Text= DateTime.Now.ToString("dd-MM-yyyy");  

I am using asp.net with c#

      start_date              end_date
8/2/2013 12:00:00 AM    10/2/2013 12:00:00 AM

Code for Storing Date in DD/MM/YYYY format

CultureInfo provider = CultureInfo.InvariantCulture;

        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 for Checking Date on login page

CultureInfo provider = CultureInfo.InvariantCulture;

        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 >= @date1 and End_date<= @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);

`if (dt.Rows.Count > 0) // comparing users from table

        {

            FormsAuthentication.RedirectFromLoginPage(txtFcode.Text, Persist.Checked);
            Response.Redirect("~/DeptCoordinator/DeptCoordinator_homepage.aspx");  
        }

Getting Error

The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar. Desc`

Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105
user2053138
  • 141
  • 2
  • 6
  • 16
  • [Duplicate query.........](http://stackoverflow.com/questions/14765629/how-to-compare-system-date-with-date-existing-in-database-table/14765663#14765663) – andy Feb 08 '13 at 06:59

1 Answers1

0

you want to synchronize the types:

convert c# date to sql format

var sqlFormattedDate = myDateTime.Date.ToString("yyyy-MM-dd HH:mm:ss");
Renaissance
  • 798
  • 5
  • 15