2016-10-31 11 views
0


GridViewに次のコードを表示しようとしていますが、わからない、または正しく動作しているかわかりません。 ... 助けてください!ASP.NET複数のテーブルから1つの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()...カレンダーからgeting値です。アドバッションでありがとう!

+0

これはSQL質問かGridView質問ですか?あなたはデータベースからデータを取得していますが、GridViewには入っていませんか、またはデータベースからデータをまったく取得していませんか? – VDWWD

+0

@VDWWDご回答いただきありがとうございます。これは、GridViewの質問とデータベースオフコースのデータです。私は "インデックスが範囲外です。これらの行では、負でない、コレクションのサイズより小さくなければなりません"というエラーを受け取ります。cmd.Parameters.AddWithValue( "@ day2"、Calendar1.SelectedDates [1] .ToShortDateString()); –

答えて

0

あなたのコードをテストしたところ正しいと思われますが、処理する必要がある問題が1つあります。ユーザがクエリに必要な7つの日付を選択しなかった場合、プログラムは 'System .ArgumentOutOfRangeException 'です。 'System.ArgumentOutOfRangeException'例外を回避するために私が提案するものは、次のものを含めることです。

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

お返事ありがとうございます。私はして、私はもうエラーメッセージが表示されませんが、私のGridViewは表示されません。私はGridViewを見ることができません。私は自分のコードを編集しました。ありがとうございました! –

関連する問題