Suppose I have an SQL table entitled “Facility” which looks like this:
|| FacilityID || Name ||
0 Lecture Theatre
1 Seminar Room
2 Computer Lab
3 Electronics Lab
... ...
and continues as such for 30-odd different room facilities. I’m trying to Databind these to a CheckBoxList in ASP.NET (C#) which looks like this:
<asp:CheckBoxList ID="FacilityCheckList" AutoPostBack="true" runat="server">
</asp:CheckBoxList>
The code I’m using to achieve this in C# looks like this:
String strSQL;
strSQL = "SELECT * FROM Facility";
SqlCommand cmd3 = new SqlCommand(strSQL, DBConnection);
DBConnection.Open();
SqlDataReader FacilityData;
FacilityData = cmd3.ExecuteReader();
FacilityData.Read();
FacilityCheckList.DataSource = FacilityData;
FacilityCheckList.DataValueField = "Name";
FacilityCheckList.DataBind();
DBConnection.Close();
Now this works absolutely fine, apart from one thing: it doesn’t display a CheckBox for the first item in the list, Lecture Theatre. Every other facility in the list shows up with a CheckBox just fine, but not Lecture Theatre – and I have no idea why!
It’s not a problem on the SQL side cos when I run the query on its own, it returns the full list. And I don’t think it’s specific to CheckBoxList, because when I tried binding to a DropDownList instead, I got the same problem. I’m pretty much stumped as to why it is doing this, so any insight would be much appreciated!
Cheers
Remove the line:
This is causing the reader to move forward one notch making you lose the first item