T
The Eeediot
Hello,
I have a drop-down list (listbox) control in ASP.NET that I would like to contain "Select Type" as the first element (text) and "0" as its value then have it followed by values from a reference table. So far I have been using the following sort of code:
Protected WithEvents lstType as ListBox
{Blah, Blah}
'Fill the lstType listbox
Private Sub FillTypeList()
'Empty the list
lstType.Items.Clear()
'Define database objects
Dim conn as New SqlConnection
Dim cmd as New SqlCommand
Dim dreader as SqlDataReader
'Define statements
conn.ConnectionString = ConnString
cmd.Connection = conn
cmd.CommandText = "SELECT * FROM GenericType ORDER BY Name"
'Connect to server and pull out data
Try
'Open connetion
'Import information to DataReader object and
'cough-up into listbox.
conn.Open()
dreader = cmd.ExecuteReader()
'Zeroth list item that says 'Select Me!!!'
Dim ZerothItem as New ListItem()
ZerothItem.Text = "Select Me!!!"
ZerothItem.Value = 0
lstIncdType.Items.Add(ZerothItem)
Do While dreader.Read()
'Populate list
Dim NewItem as New ListItem()
NewItem.Text = dreader("Name")
NewItem.Value = dreader("Idx")
lstIncdType.Items.Add(NewItem)
Loop
dreader.Close()
Catch err as Exception
'To err is human...bail-out!
lblWarnings.Style("color") = "red"
lblWarnings.Text = "Error reading the database. "& err.Message
Finally
'close connection
If NOT(IsNothing(conn)) Then
conn.Close()
End If
End Try
End Sub
How could I use DataBinding with this one?
TIA...
I have a drop-down list (listbox) control in ASP.NET that I would like to contain "Select Type" as the first element (text) and "0" as its value then have it followed by values from a reference table. So far I have been using the following sort of code:
Protected WithEvents lstType as ListBox
{Blah, Blah}
'Fill the lstType listbox
Private Sub FillTypeList()
'Empty the list
lstType.Items.Clear()
'Define database objects
Dim conn as New SqlConnection
Dim cmd as New SqlCommand
Dim dreader as SqlDataReader
'Define statements
conn.ConnectionString = ConnString
cmd.Connection = conn
cmd.CommandText = "SELECT * FROM GenericType ORDER BY Name"
'Connect to server and pull out data
Try
'Open connetion
'Import information to DataReader object and
'cough-up into listbox.
conn.Open()
dreader = cmd.ExecuteReader()
'Zeroth list item that says 'Select Me!!!'
Dim ZerothItem as New ListItem()
ZerothItem.Text = "Select Me!!!"
ZerothItem.Value = 0
lstIncdType.Items.Add(ZerothItem)
Do While dreader.Read()
'Populate list
Dim NewItem as New ListItem()
NewItem.Text = dreader("Name")
NewItem.Value = dreader("Idx")
lstIncdType.Items.Add(NewItem)
Loop
dreader.Close()
Catch err as Exception
'To err is human...bail-out!
lblWarnings.Style("color") = "red"
lblWarnings.Text = "Error reading the database. "& err.Message
Finally
'close connection
If NOT(IsNothing(conn)) Then
conn.Close()
End If
End Try
End Sub
How could I use DataBinding with this one?
TIA...