2014-09-28 100 views
0

我试图通过在代码中输入事件来使日历正常工作。我对ASP.NET还不熟悉,所以我不知道如何解决这个问题。我一直得到的错误是:“事件”不包含一个带7个参数的构造函数。事件错误:参考事件构造函数

protected void Page_Load(object sender, EventArgs e) 
{ 

    DateTime d1; 

    anEvent[0] = new Event("Event1", "Event one", "08/01/2014", "20.00", "Connie", "222-222-2222", "[email protected]"); 

    anEvent[1] = new Event("Event2", "Event two", "08/15/2014", "80.00", "Cathryn", "555-555-5555", "[email protected]"); 

    anEvent[2] = new Event("Event3", "Event three", "09/05/2014", "50.00", "Charles", "888-888-8888", "[email protected]"); 

    foreach (Event myEvent in anEvent) 
    { 

     String anEventDate; 

     anEventDate = myEvent.EventDate; 

     d1 = Convert.ToDateTime(anEventDate); 

     Calendar2.SelectedDates.Add(d1); 

    } 
} 







using System; 

using System.Collections.Generic; 

using System.Linq; 

using System.Web; 

using System.Web.UI; 

using System.Web.UI.WebControls; 

using System.Configuration; 

using System.Data.SqlClient; 

using System.Data; 





public partial class CurrentEvents : System.Web.UI.Page 
{ 

Event[] anEvent = new Event[3]; 

protected void Page_Load(object sender, EventArgs e) 
{ 

    DateTime d1; 

    anEvent[0] = new Event("Event1", "Event one", "08/01/2014", "20.00", "Connie", "222-222-2222", "[email protected]"); 

    anEvent[1] = new Event("Event2", "Event two", "08/15/2014", "80.00", "Cathryn", "555-555-5555", "[email protected]"); 

    anEvent[2] = new Event("Event3", "Event three", "09/05/2014", "50.00", "Charles", "888-888-8888", "[email protected]"); 



    foreach (Event myEvent in anEvent) 
    { 

     String anEventDate; 



     anEventDate = myEvent.EventDate; 

     d1 = Convert.ToDateTime(anEventDate); 

     Calendar2.SelectedDates.Add(d1); 

    } 



} 



protected void Calendar2_DayRender(object sender, DayRenderEventArgs e) 
{ 

    String todayDate = DateTime.Today.ToString("MM/dd/yyyy"); ; 

    String calendarDate; 

    for (int i = 0; i < Calendar2.SelectedDates.Count; i++) 
    { 



     calendarDate = Calendar2.SelectedDates[i].ToString("MM/dd/yyyy"); 

     if (e.Day.Date.CompareTo(Calendar2.SelectedDates[i]) == 0) 
     { 

      if (calendarDate.CompareTo(todayDate) < 0) 
      { 

       e.Cell.BackColor = System.Drawing.Color.Red; 



      } 

      else 

       if (calendarDate.CompareTo(todayDate) > 0) 
       { 

        e.Cell.BackColor = System.Drawing.Color.Blue; 



       } 

       else 

        if (calendarDate.CompareTo(todayDate) == 0) 
        { 

         e.Cell.BackColor = System.Drawing.Color.Green; 

        } 

        else 
        { 

         e.Cell.BackColor = System.Drawing.Color.LightGray; 

        } 

     } 

    } 

} 



protected void Calendar2_SelectionChanged(object sender, EventArgs e) 
{ 

    String selectedDate; 

    int foundDate = 0; 



    selectedDate = Calendar2.SelectedDate.ToString("MM/dd/yyyy"); 



    foreach (Event myEvent in anEvent) 
    { 

     if (selectedDate.Equals(myEvent.EventDate)) 
     { 

      this.ControlNewEventControl.updateControl(sender, e, myEvent.EventName, myEvent.EventDescription, myEvent.EventDate, myEvent.EventFee, myEvent.ECordNm, myEvent.ECordNum, myEvent.ECordEml); 

      ErrorLabel.Text = ""; 

      foundDate = 1; 

     } 



    } 

    if (foundDate == 0) 
    { 

     this.ControlNewEventControl.updateControl(sender, e, "", "", "", "", "", "", ""); 

     ErrorLabel.Text = "* There is no Event Scheduled for that Date.<br />&nbsp;&nbsp;&nbsp;&nbsp;Please Choose Another Date."; 

    } 



    Page_Load(sender, e); 

} 



    /* SqlConnection dbConnection = new SqlConnection("Data Source=(LocalDB)\\v11.0;AttachDbFilename=|DataDirectory|\\Database2.mdf;Integrated Security=True"); 

    DateTime Date; 



    try 
    { 

     dbConnection.Open(); 

     string SQLselectQuery = "SELECT EventDate FROM Event"; 

     using (SqlCommand selectCommand = new SqlCommand(SQLselectQuery, dbConnection)) 
     { 

      SqlDataReader reader = selectCommand.ExecuteReader(); 

      while (reader.Read()) 
      { 

       Date = reader.GetDateTime(0); 

       Calendar2.SelectedDates.Add(Date); 

      } 

     } 

    } 

    catch (SqlException exception) 
    { 

     Response.Write("<p>Error code " + exception.Number + ": " + exception.Message + "</p>"); 

    } 

    finally 
    { 

     dbConnection.Close(); 

    } 

} 



protected void Calendar2_DayRender(object sender, DayRenderEventArgs e) 
{ 

    String todayDate = DateTime.Today.ToString("MM/dd/yyyy"); ; 

    String calendarDate; 

    for (int i = 0; i < Calendar2.SelectedDates.Count; i++) 
    { 



     calendarDate = Calendar2.SelectedDates[i].ToString("MM/dd/yyyy"); 

     if (e.Day.Date.CompareTo(Calendar2.SelectedDates[i]) == 0) 
     { 

      if (calendarDate.CompareTo(todayDate) < 0) 
      { 

       e.Cell.BackColor = System.Drawing.Color.Red; 

      } 

      else if (calendarDate.CompareTo(todayDate) > 0) 
      { 

       e.Cell.BackColor = System.Drawing.Color.Blue; 

      } 

      else if (calendarDate.CompareTo(todayDate) == 0) 
      { 

       e.Cell.BackColor = System.Drawing.Color.Green; 

      } 

      else 
      { 

       e.Cell.BackColor = System.Drawing.Color.Fuchsia; 

      } 

     } 

    } 

} 



protected void Calendar2_SelectionChanged(object sender, EventArgs e) 
{ 

    SqlConnection dbConnection = new SqlConnection("Data Source=(LocalDB)\\v11.0;AttachDbFilename=|DataDirectory|\\Database2.mdf;Integrated Security=True"); 



    try 
    { 

     int count = 0; 

     dbConnection.Open(); 

     string SQLselectQuery = "SELECT * FROM Event WHERE EventDate = @EventDate"; 

     SqlCommand selectCommand = new SqlCommand(SQLselectQuery, dbConnection); 

     selectCommand.Parameters.AddWithValue("@EventDate", Calendar2.SelectedDate.ToString()); 

     using (selectCommand) 
     { 

      SqlDataReader reader = selectCommand.ExecuteReader(); 

      while (reader.Read()) 
      { 

       count++; 

       Ename.Text = reader.GetString(1).ToString(); 

       Escript.Text = reader.GetString(2).ToString(); 

       EDate.Text = reader.GetDateTime(3).ToString(); 

       EvFee.Text = reader.GetDecimal(4).ToString(); 

       ECordNm.Text = reader.GetString(5).ToString(); 

       ECordNum.Text = reader.GetString(6).ToString(); 

       ECordEml.Text = reader.GetString(7).ToString(); 

      } 

     } 

     if (count == 0) 
     { 

      Ename.Text = ""; 

      Escript.Text = "There is no Event for this Date"; 

      EDate.Text = Calendar2.SelectedDate.ToString(); 

      EvFee.Text = ""; 

      ECordNm.Text = ""; 

      ECordNum.Text = ""; 

      ECordEml.Text = ""; 

     } 

    } 

    catch (SqlException exception) 
    { 

     Response.Write("<p>Error code " + exception.Number + ": " + exception.Message + "</p>"); 

    } 

    finally 
    { 

     dbConnection.Close(); 

     Page_Load(sender, e); 

    } 

} */ 

protected void Ename_TextChanged(object sender, EventArgs e) 
{ 

} 
} 
+0

错误很明显 - “Event”类没有一个构造函数需要7个参数。发布'Event'的代码(如果它是你自己的课程),我们可以告诉你更多。 – Tim 2014-09-28 20:03:05

+0

显示您的班级 – meda 2014-09-28 20:11:17

+0

我不太确定要发布哪些代码。我是否发布主aspx页面? – 2014-09-28 20:12:53

回答

0

看来,你发布的代码是不完整的 - 地方必须有类Event的定义,否则你不会得到你所看到的错误。

错误消息本身很明显 - 类Event没有带7个参数的构造函数。从您发布的代码推断,我希望有在你的项目中一类的地方(或其他引用的项目),它可能是这个样子:

public class Event 
{ 

    public string EventID { get; set; } 

    public string EventName { get; set; } 

    public string EventDate { get; set; } 

    public string EventFee { get; set; } 

    public string EventCoordinator { get; set; } 

    public string EventCoordinatorSSN { get; set; } 

    public string EventCoordinatorEmail { get; set; } 
} 

这只是一种猜测 - 它可能是例如,设置私有字段(即private string eventDate等)而不是属性。这里的重要项目是你有一个类/属性的类,但它只有一个默认(无参数)的构造函数。

所以,当你尝试做到以下几点:new Event("Event1", "Event one", "08/01/2014", "20.00", "Connie", "222-222-2222", "[email protected]"),编译器会在Event类接受7个字符串参数的构造函数,但它不能找到一个(因此错误)。

有几种方法可以解决这个问题,这取决于类是如何设计的。你可以添加一个构造函数7个字符串参数,并将它们分配到7场,这样的:

public Event(string ID, string name, string date, string fee, string coordinator, string ssn, string email) 
{ 

    eventID = ID; 
    eventName = name; 
    eventDate = date; 
    eventFee = fee; 
    eventCoordinator = coordinator; 
    eventCoordinatorSSN = ssn; 
    eventCoordinatorEmail = email; 
} 

这将7个参数分配给7个各自的领域。

另一种选择是使用自动属性和对象初始化。这将允许您初始化通过自动属性(见我最初张贴在这个答案的代码)的领域,应该是这样的:

anEvent[0] = new Event() { 
       EventID = "Event1", 
       EventName = "Event one", 
       EventDate = "08/01/2014", 
       EventFee = "20.00", 
       EventCoordinator = "Connie", 
       EventCoordinatorSSN = "222-22-2222", 
       EventCoordintaorEmail = "[email protected]" 
}; 

这将创建Event类的新实例,并分配通过属性为字段提供值。

还有一些其他的问题,在这里(如使用string的一切,而不是DateTime日期,decimal货币等),但由于这是一类项目的教练可能不会得到该还,所以我们可以只要坚持string

没有看到Event类的代码,我们不能给你更详细的帮助,但希望这个答案能给你一些关于正在发生什么和在哪里寻找的想法。