【发布时间】:2015-06-15 14:34:52
【问题描述】:
这是我第二次使用 VB.net,所以我不确定语言。 我正在尝试从标题为“类别”的表列中创建列表框值。有重复,所以我试图只显示每个类别一次。我必须这样做,因为用户可以添加更多类别,这意味着我的列表框值需要动态更新。
我不确定我是否正确地执行此过程:
Protected Sub CategoryListBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CategoryListBox.SelectedIndexChanged
Dim conn As OleDbConnection = New OleDbConnection("Provider=""*******"";user id=" & strUserID & ";data source=" & strDatabase & ";password=" & strPssWd)
Dim ClassifiedStr As OleDbCommand = New OleDbCommand("SELECT classid, Addate, username, category, description FROM t_classifieds", conn)
Dim OracleDataAdapterAds As OleDbDataAdapter = New OleDbDataAdapter
OracleDataAdapterAds.SelectCommand = ClassifiedStr
Dim DsAds As DataSet = New DataSet
DsAds.Clear()
OracleDataAdapterAds.Fill(DsAds, "t_classifieds")
CategoryListBox.DataTextField = "category"
CategoryListBox.DataValueField = "category"
CategoryListBox.DataSource = DsAds
CategoryListBox.DataMember = "t_classifieds"
CategoryListBox.DataBind()
Dim Categories As String
Dim CategoryID
For Each dr As DataRow In DsAds.Tables("t_classifieds").Rows
Categories = dr("category").ToString()
CategoryID = dr("classid").ToString()
CategoryListBox.Items.Add(Categories)
Next dr
conn.Close()
End Sub
我查看了其他示例,并尝试镜像它们,但我的列表框中没有打印任何内容。
如果我使用预定义的 Dim 值,我的代码的第二个版本可以工作,但由于某种原因,我没有获得数据库连接来显示数据库类别列表。
第二版:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
FillList()
End Sub
Private Sub FillList()
Dim conn As OleDbConnection = New OleDbConnection("Provider=""MSDAORA.1"";user id=" & strUserID & ";data source=" & strDatabase & ";password=" & strPssWd)
Dim data_reader As OleDbDataReader
Dim ClassifiedStr As OleDbCommand = New OleDbCommand("SELECT * FROM t_classifieds ", conn)
Dim Categories
conn.Open()
data_reader = ClassifiedStr.ExecuteReader()
CategoryListBox.Items.Clear()
If data_reader.HasRows Then
Do While data_reader.Read()
Categories = data_reader.Item("category")
'CategoryListBox.Items.Add(New ListItem(Categories))
CategoryListBox.Items.Add(Categories.ToString())
Loop
End If
data_reader.Close()
data_reader = Nothing
ClassifiedStr.Dispose()
ClassifiedStr = Nothing
conn.Close()
conn.Dispose()
'Dim dWorkDate As Date = CDate("01.01.2014")
'While dWorkDate < Date.Today
'CategoryListBox.Items.Add(dWorkDate.ToString("dd.MM.yyyy"))
'dWorkDate = dWorkDate.AddDays(1)
'End While
End Sub
【问题讨论】:
标签: asp.net vb.net listbox webpage