2017-09-20 7 views
0

私はCheckBoxListを持っており、編集ボタンでチェックしたいと思います。データベースの列に値がある場合、それ以外の場合はチェックされません。チェックボックスリストの値はデータベースからのものです。データベースを使用してチェックボックスリストをチェックする方法

それはエラーを示しています。

Index was out of range. Must be non-negative and less than the size of the collection.

のデバッグ中:

Chk_seat.Items[j].Selected = true; 

はどのように私はそれを行うことができますか?

<asp:CheckBoxList ID="Chk_seat" runat="server" RepeatDirection="Horizontal"> 
</asp:CheckBoxList> 

public void updatefields_seat(long CarID) 
{ 
    SqlConnection cn = new SqlConnection(connection); 
    string qry = @"SELECT [ID] 
       ,[ReferenceID] 
       ,[CarID] 
       ,[Child-Seat] 
       ,[Leather-Seat] 
       ,[Power-Seat] 
       ,[Navigation-System] 
       ,[Power-Steering] 
       ,[Bucket-Seat] 
       ,[IsActive] 
       FROM [dbo].[Seat_tbl] Where CarID=" + CarID + ""; 

    DataSet dsSeat = new DataSet(); 
    dsSeat = cm.GetQryInDataset(qry); 

    if (dsSeat.Tables.Count > 0) 
    { 
     for (int i = 0; i < dsSeat.Tables[0].Rows.Count; i++) 
     { 
      for (int j = 0; j < dsSeat.Tables[0].Columns.Count; j++) 
      { 
       if (dsSeat.Tables[0].Rows[i]["Child-Seat"].ToString() != "") 
       { 
        Chk_seat.Items[j].Selected = true; 
       } 
      } 
     } 
    } 
} 
+0

Asif.Ali

は、ここに私のコードです。ありがとう兄貴。私はあなたのコメントを読んだ後にその問題を解決しました。私はdidntの前に問題を得る。 –

答えて

0

dsSeat.Tables[0].Columns.Countあなたはこのエラーを得ている理由ですあなたのCheckBoxListのサイズ/長さよりも大きいことができます:

Index was out of range . Must be non-negative and less than the size of the collection.

関連する問題