Developing a Data Access Layer for Sybase using ADO.NET: Essentials Continued - Binding a dropdown list with information from Sybase database using ADO.NET: explanation
(Page 2 of 4 )
This section mainly deals with the explanation of the code listed in the previous section. Let me explain it part by part:
Dim dt As DataTable = getDataTable(sqlSELECT)
If dt.Columns.Count < 2 Then
Throw New Exception("Atleast 2 cols are required
to bind, Given SQL Statement: " & sqlSELECT)
End If
The above part mainly retrieves the information based on the SELECT statement passed to it. It also checks whether the information is retrieved in the form of two columns or not. If not, it would raise an error.
If ClearExistingItems Then dl.Items.Clear()
If Not Caption Is Nothing Then
dl.Items.Add(New ListItem(Caption, Caption))
dl.Items.Add(New ListItem("-".PadLeft(150, "-"),
Caption))
End If
The above part first tries to clear the dropdown list based on the parameter we pass. If we provide some “caption” to display, then it would be added as the first element along with a separator. Further proceeding, we have the following:
Dim dr As DataRow
If ShowIDandDESCSeparated Then
For Each dr In dt.Rows
dl.Items.Add(New ListItem(dr(0) & " - " & dr
(1) & "", dr(0) & ""))
Next
Else
For Each dr In dt.Rows
dl.Items.Add(New ListItem(dr(1) & "", dr(0) &
""))
Next
End If
dl.SelectedIndex = -1
The above part gets divided into two parts. One is related to adding items with both key and description and the other is related to adding items with only description, without the key. One of the parts gets executed based on the parameter we specify for “ShowIDandDESCSeparated.” The last statement is mainly used to make the dropdown list point to no item after binding.
Next: Getting a dataset of information from Sybase database using ADO.NET >>
More ASP.NET Articles
More By Jagadish Chaterjee