如何在ASP.net中创建节日日历

时间:2011-09-14 05:59:39

标签: c# asp.net sql-server-2005

我想使用asp.net创建一个节日日历,因为我使用了两个ajax日历和一个文本框,它是一个节日文本框,我们分别进入了FromDate和ToDate的节日。我想这样做如下点

  1. 如果我进入文本框圣诞节并选择Fromdate = 25/12/2011和ToDate = 31/12/2011那么它将是有效的

  2. 如果我选择日期= 25/12/2011和ToDate = 24/12/2011那么它将无效

  3. 如果我选择Fromdate = 25/12/2011和Todate = 28/12/2011那么它也是无效的,因为它发生在2011年12月25日至2011年12月31日之间

    < / LI>
  4. 如果我选择fromdate = 1/1/2011和ToDate = 1/1/2011,那么它是有效的

  5. 如果我选择日期= 2011年12月21日和2011年12月25日,那么因为已经在2011年1月1日完成圣诞节而无效

  6. 并且所有日期都应该在gridview中显示为25-dec-2011格式

    这是我的代码:

    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.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";
    
    }
    

2 个答案:

答案 0 :(得分:1)

此代码应该起作用

     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; }
    }

答案 1 :(得分:0)

这不是完整的语法,但应该可以帮助您了解您需要做什么:

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
}