简体   繁体   English

VB.NET下拉列表未填充所有项目

[英]VB.NET dropdown list not populating with all items

I am populating an asp dropdown list by reading data from a MySQL table via stored procedure. 我正在通过存储过程从MySQL表读取数据来填充asp下拉列表。

The data reader returns all 4 rows from the table however only rows 2, 3, 4 are listed in the dropdown list. 数据读取器返回表中的所有4行,但是下拉列表中仅列出了2、3、4行。

This is the code that sets the dropdown list. 这是设置下拉列表的代码。 Does anyone have any ideas on what may have gone wrong? 是否有人对可能出了什么问题有任何想法?

Thanks 谢谢

 Try
        Dim conn As New MySql.Data.MySqlClient.MySqlConnection(myConnectionString)
        Dim cmd As New MySqlCommand()

        conn.Open()
        cmd.Connection = conn
        cmd.CommandText = "Select_CatLibraryData"
        cmd.CommandType = CommandType.StoredProcedure

        Using catdata_rs As MySqlDataReader = cmd.ExecuteReader()

            If catdata_rs.Read() Then
                list_itemcategory.DataSource = catdata_rs
                list_itemcategory.DataValueField = "category"
                list_itemcategory.DataTextField = "category"
                list_itemcategory.DataBind()
            End If

        End Using

    Catch ex As MySql.Data.MySqlClient.MySqlException

        lbl_ErrorPanelText.Text = "Error establishing a database connection!"
        pn_ErrorPanel.Visible = True

    End Try

You should not use If catdata_rs.Read() since it advances the pointer by 1. 您不应该使用If catdata_rs.Read()因为它会将指针前进1。

Instead use code like below. 而是使用如下代码。 Or you could just remove the surrounding if around databinding code and directly bind to data reader and if its empty then your list drop down will also be empty. 或者你可以只取出周围if周围的数据绑定代码,并直接绑定到数据读取器,如果它的空然后将列表下拉也将是空的。

If catdata_rs.HasRows Then
            list_itemcategory.DataSource = catdata_rs
            list_itemcategory.DataValueField = "category"
            list_itemcategory.DataTextField = "category"
            list_itemcategory.DataBind()
End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM