I have 2 columns in my table TimeTable
start_date
(format isDD-MM-YYYY
)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`
Best Answer
Assuming that you've loaded start_date and end_from from the table. Then