ASP.NET在多个表中的一个GridView中显示数据

时间:2016-10-31 17:38:49

标签: c# asp.net gridview


我正在尝试在GridView中显示以下代码,我不知道该怎么做,或者我不知道我做得对...... 请帮忙!

string cs = ConfigurationManager.ConnectionStrings["MyDb"].ConnectionString;

using (SqlConnection connection = new SqlConnection(cs))
{

    SqlCommand cmd = new SqlCommand((@"select e.FirstName,
                        count(case when v.VacationDates = @day1 THEN 1 END),
                        count(case when v.VacationDates = @day2 THEN 1 END),
                        count(case when v.VacationDates = @day3 THEN 1 END),
                        count(case when v.VacationDates = @day4 THEN 1 END),
                        count(case when v.VacationDates = @day5 THEN 1 END),
                        count(case when v.VacationDates = @day6 THEN 1 END),
                        count(case when v.VacationDates = @day7 THEN 1 END)
                        from Employee e

                        left join Vacation v
                        on e.EmployeeId = v.EmployeeId
                        group by e.FirstName"), connection) ;

 if (Calendar1.SelectedDates.Count > 6)
                {
                    cmd5.Parameters.AddWithValue("@day1", Calendar1.SelectedDates[0].ToShortDateString());
                    cmd5.Parameters.AddWithValue("@day2", Calendar1.SelectedDates[1].ToShortDateString());
                    cmd5.Parameters.AddWithValue("@day3", Calendar1.SelectedDates[2].ToShortDateString());
                    cmd5.Parameters.AddWithValue("@day4", Calendar1.SelectedDates[3].ToShortDateString());
                    cmd5.Parameters.AddWithValue("@day5", Calendar1.SelectedDates[4].ToShortDateString());
                    cmd5.Parameters.AddWithValue("@day6", Calendar1.SelectedDates[5].ToShortDateString());
                    cmd5.Parameters.AddWithValue("@day7", Calendar1.SelectedDates[6].ToShortDateString());




                        connection.Open();

                        GridView12.DataSource = cmd5.ExecuteReader();
                        GridView12.DataBind();


               }
}

Calendar1.SelectedDates [0] .ToShortDateString() Calendar1.SelectedDates [1] .ToShortDateString()....是一个从Calendar开始的值。谢谢你的建议!

1 个答案:

答案 0 :(得分:0)

我测试了您的代码,看起来是正确的,但是您需要处理一个问题,如果用户没有选择查询所需的七(7)个日期将抛出一个' System.ArgumentOutOfRangeException'。我建议你为了避免'System.ArgumentOutOfRangeException'例外是包括:

if (Calendar1.SelectedDates.Count > 6)
{
(your code)
}