I want to make a festival calendar using asp.net from that I used two ajax calendar and one textbox it is a festival textbox where we enter festival which FromDate and ToDate respectively. I want to do this as following point
If I enter in textbox Christmas and Choose Fromdate=25/12/2011 and ToDate=31/12/2011 then it will be valid
If I choose fromDate=25/12/2011 and ToDate=24/12/2011 then it will invalid
If I choose Fromdate=25/12/2011 and Todate=28/12/2011 then also it is invalid because it coming in between 25/12/2011 and 31/12/2011
If I Choose fromdate=1/1/2011 and ToDate=1/1/2011 then it is valid
If I choose fromdate=21/12/2011 and 25/12/2011 then it is invalid because of already Christmas done in 1/1/2011
And all date should show in gridview like 25-dec-2011 format
Here is my code:
DateTime dt1 = Convert.ToDateTime(txt_fromdate.Text);
DateTime dt2 = Convert.ToDateTime(txt_todate.Text);
if (dt1 > dt2)
{
con.Open();
com = new SqlCommand("BTNN_MovieDB_Festival_Details_Insert", con);
com.Parameters.Add("@fromdate", SqlDbType开发者_StackOverflow社区.VarChar).Value = dateformat_mmdd(txt_fromdate.Text.ToString().Trim());
com.Parameters.Add("@todate", SqlDbType.VarChar).Value = dateformat_mmdd(txt_todate.Text.ToString().Trim());
com.Parameters.Add("@return", SqlDbType.VarChar).Direction = ParameterDirection.ReturnValue;
com.ExecuteNonQuery();
con.Close();
showdata();
}
else if (dt1 < dt2)
{
lblerror.Text = "ToDate should be greater than FromDate";
}
This code should function
static void Main(string[] args)
{
// allFestivals holds all the festival list
// allFestivals values may come from some other data source like database
List<Festival> allFestivals =new List<Festival>();
// Simulate by inserting Christmas
Festival chirstmas = new Festival()
{
Name = "Christmas",
startDate = new DateTime(2011, 12, 25),
endDate = new DateTime(2011, 12, 31)
};
AddFestival(allFestivals, chirstmas);
// NewYear will not be inserted since 31-12-2011 is already
// marked as holiday by Christmas
AddFestival(allFestivals, new Festival()
{
Name = "NewYear",
startDate = new DateTime(2011, 12, 31),
endDate = new DateTime(2012, 1, 01)
});
Console.ReadLine();
}
/// <summary>
/// Add new festival to the list of festivals
/// </summary>
/// <param name="allFestivals"></param>
/// <param name="newFestival"></param>
static void AddFestival(List<Festival> allFestivals, Festival newFestival)
{
// If newFestival meets all the criteria only then add to the list
if (ValidDates(newFestival) &&
!NameExists(allFestivals, newFestival) &&
!NonHoliday(allFestivals, newFestival) )
{
// allFestivals values may be strored into database
allFestivals.Add(newFestival);
}
}
/// <summary>
/// Check if the newFestival startDate or endDate falls within any of the already
/// existing festival start and end date
/// </summary>
/// <param name="dates"></param>
/// <param name="newFestival"></param>
/// <returns></returns>
private static bool NonHoliday(List<Festival> dates, Festival newFestival)
{
return dates.Exists((date) =>
newFestival.startDate >= date.startDate && newFestival.startDate <= date.endDate ||
newFestival.endDate >= date.startDate && newFestival.endDate <= date.endDate);
}
/// <summary>
/// If the festival name already exists, returns true else false
/// </summary>
/// <param name="dates"></param>
/// <param name="newFestival"></param>
/// <returns></returns>
private static bool NameExists(List<Festival> dates, Festival newFestival)
{
return dates != null &&
dates.Count() > 0 &&
dates.FirstOrDefault((dt) => dt.Name == newFestival.Name) != null;
}
/// <summary>
/// Validate if end date is greater than or equal to start date
/// </summary>
/// <param name="newFestival"></param>
/// <returns></returns>
private static bool ValidDates(Festival newFestival)
{
return newFestival.endDate >= newFestival.startDate;
}
// Data structure represenging festival details
class Festival
{
public DateTime startDate { get; set; }
public DateTime endDate { get; set; }
public string Name { get; set; }
}
This isn't the full syntax, but should help you see what you need to do:
DateTime d1 = txtDate1.Text;
DateTime d2 = txtDate2.Text;
if (d1 < d2){
//Invalid
}
SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM Table WHERE ([FromDate]<@Date1 AND [ToDate]>@Date1) OR (([FromDate]<@Date2 AND [ToDate]>@Date2)", conn);
//TODO: Add Parameters
if (((int)cmd.executescalar())>0){
//Invalid
}
精彩评论