This is my second experience with VB.net so I am not sure about the language. I am trying to create the listbox values from my table column titled "category". There are duplicates so I am trying to only display each category once. I have to do it this way because the user's can add more categories which means my listbox values need to dynamically update.
I am not sure I am doing this process right:
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
I have looked at other examples and I have tried to mirror them but nothing prints in my listbox.
My second version of my code works if I use a predefined Dim value but for some reason I am not getting the database connection to display the database Category listing.
Second Version:
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
答案 0 :(得分:1)
Select DISTINCT
values for the category:
Dim ClassifiedStr As OleDbCommand = New OleDbCommand("SELECT DISTINCT category FROM t_classifieds", conn)
'...
CategoryListBox.DataSource = DsAds.Tables("t_classifieds")
CategoryListBox.DataTextField = "category"
CategoryListBox.DataValueField = "category"
By the way, you don't need to call Clear
on a New DataSet
.
答案 1 :(得分:0)
实现了我的旧代码,但我需要@Keith Sql代码。
If Not IsPostBack Then
If Not CategoryListBox Is Nothing Then
Dim conn As OleDbConnection = New OleDbConnection("Provider=""****"";user id=" & strUserID & ";data source=" & strDatabase & ";password=" & strPssWd)
'Display All Classified Ads listed in the Database based on the following format/order
'Date Name Home Phone Number Description
Dim ClassifiedStr As OleDbCommand = New OleDbCommand("SELECT DISTINCT category 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.DataSource = DsAds
CategoryListBox.DataMember = "t_classifieds"
CategoryListBox.DataBind()
End If
End If
我只有一个随机空间:/