locked
Populate a DropDownList from Code Behind RRS feed

  • Question

  • User-1352156089 posted

    Hi all,

    I cannot believe it but I cannot understand why my dropdownlist control is not populated from db. This is my code:

    <asp:DropDownList ID="ddlcontinents" runat="server" AppendDataBoundItems="true">
      <asp:ListItem Text="Select Continent" Value="" />
        </asp:DropDownList

    and in code behind:

    Imports System.Data.SqlClient
    Imports System.Data
    
    Partial Class admin_ddl
        Inherits System.Web.UI.Page
        Protected Sub Page_Load(sender As Object, e As EventArgs)
            If IsPostBack Then
    
                PopulateDDL()
    
            End If
    
        End Sub
        Public Sub PopulateDDL()
            Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("DefaultConnection").ConnectionString)
            Dim cmdText As String = "SELECT ContinentId, ContinentName FROM Continents"
            Dim cmd As New SqlCommand(cmdText, con)
            If con.State = ConnectionState.Closed Then
                con.Open()
            End If
            ddlcontinents.DataSource = cmd.ExecuteReader()
            ddlcontinents.DataValueField = "ContinentId"
            ddlcontinents.DataTextField = "ContinentName"
            ddlcontinents.DataBind()
        End Sub
    End Class

    What's missing in your opinion?

    Thanks

    Friday, May 8, 2015 10:21 AM

Answers

  • User61956409 posted

    Hi claudio7810,

    Thanks for your post.

    According to your description, I create the following sample to reproduce your problem, the code works fine on my side.

    Data In “Continents” table:

    Web page:

    <div>
        <asp:DropDownList ID="ddlcontinents" runat="server" AppendDataBoundItems="true">
            <asp:ListItem Text="Select Continent" Value="" />
        </asp:DropDownList>
    </div> 
    

    Code behind:

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                PopulateDDL()
            End If
        End Sub
    Public Sub PopulateDDL() Dim con As New SqlConnection("myconnectionstring") Dim queryString As String = "SELECT ContinentId, ContinentName FROM Continents" con.Open() Dim cmd As New SqlCommand(queryString, con) ddlcontinents.DataSource = cmd.ExecuteReader() ddlcontinents.DataValueField = "ContinentId" ddlcontinents.DataTextField = "ContinentName" ddlcontinents.DataBind() End Sub

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 11, 2015 4:52 AM

All replies

  • User1577371250 posted

    Hi,

    You need to populate the DropdonList inside the postback.

    Protected Sub Page_Load(sender As Object, e As EventArgs)
    
    	If Not IsPostBack Then
    
    		PopulateDDL()
    	End If
    
    End Sub
    

    Friday, May 8, 2015 10:26 AM
  • User-1352156089 posted

    Hi Lokesh,

    thanks but I also tried your method and it doesn't work. The DLL still doesn't get populated.

    There should be something else missing.

    Thanks

    Friday, May 8, 2015 11:31 AM
  • User1577371250 posted

    Hi,

    Everything looks fine and here is the sample code without database.

    Protected Sub Page_Load(sender As Object, e As EventArgs)
    	If Not IsPostBack Then
    		PopulateDDL()
    	End If
    End Sub
    
    Public Sub PopulateDDL()
    	Dim dt As New DataTable()
    	dt.Columns.Add("Name")
    	dt.Columns.Add("Country")
    
    	dt.Rows.Add("Scott", "USA")
    	dt.Rows.Add("Tom", "UK")
    	dt.Rows.Add("Amy", "AUS")
    	dt.Rows.Add("John", "IND")
    
    	ddlcontinents.DataSource = dt
    	ddlcontinents.DataValueField = "Country"
    	ddlcontinents.DataTextField = "Name"
    	ddlcontinents.DataBind()
    End Sub
    
      <asp:DropDownList ID="ddlcontinents" runat="server" AppendDataBoundItems="true">
                    <asp:ListItem Text="Select Continent" Value="" />
                </asp:DropDownList>



    Friday, May 8, 2015 11:52 AM
  • User-1352156089 posted

    Very strange....there is should be something in my application that prevents the ddl to be populated.

    I checked everything, code, select statement and everything looks good.

    I need to double check what's wrong.

    Thanks

    Friday, May 8, 2015 12:01 PM
  • User61956409 posted

    Hi claudio7810,

    Thanks for your post.

    According to your description, I create the following sample to reproduce your problem, the code works fine on my side.

    Data In “Continents” table:

    Web page:

    <div>
        <asp:DropDownList ID="ddlcontinents" runat="server" AppendDataBoundItems="true">
            <asp:ListItem Text="Select Continent" Value="" />
        </asp:DropDownList>
    </div> 
    

    Code behind:

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                PopulateDDL()
            End If
        End Sub
    Public Sub PopulateDDL() Dim con As New SqlConnection("myconnectionstring") Dim queryString As String = "SELECT ContinentId, ContinentName FROM Continents" con.Open() Dim cmd As New SqlCommand(queryString, con) ddlcontinents.DataSource = cmd.ExecuteReader() ddlcontinents.DataValueField = "ContinentId" ddlcontinents.DataTextField = "ContinentName" ddlcontinents.DataBind() End Sub

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 11, 2015 4:52 AM