Results 1 to 7 of 7

Thread: help with populating dropdownlist in datalist

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2002
    Location
    Finland
    Posts
    15

    Question help with populating dropdownlist in datalist

    Hi!
    i just don't know how to do this. can anyone help me, please.

    i'm trying to populate dropdownlist which is in datalist. Data should come from sql database. Here is the dropdownlist:

    VB Code:
    1. <asp:DropDownList ID="dropList" Runat="server" DataTextField="place_name" DataValueField="place_id"></asp:DropDownList>

    and here is what i have tried:

    VB Code:
    1. Sub BindData(ByVal intCompany_id As Integer)
    2.         Dim sqlConnStr As String = ConfigurationSettings.AppSettings("ConnectionString")
    3.         Dim sqlConn As New SqlConnection(sqlConnStr)
    4.         Dim sqlSelect As String = "SELECT * FROM Places WHERE place_company_id=" & intCompany_id
    5.  
    6.         Dim sqlCmd As SqlCommand
    7.         sqlConn.Open()
    8.         Dim sqlDR As SqlDataReader
    9.  
    10.         sqlCmd = New SqlCommand(sqlSelect, sqlConn)
    11.         sqlDR = sqlCmd.ExecuteReader()
    12.  
    13.         While sqlDR.Read()
    14.             Dim newListItem As New ListItem
    15.             newListItem.Text = sqlDR.Item("place_name")
    16.             newListItem.Value = sqlDR.Item("place_id")
    17.             dropList.Items.Add(newListItem)
    18.         End While
    19.  
    20.  
    21.         sqlConn.Close()
    22.     End Sub

    I don't know how to catch the right dropdownlist object in datalist to populate. any ideas? anyone?

    Thanks in advance
    - Minna

  2. #2
    Fanatic Member
    Join Date
    Jun 2005
    Posts
    625

    Re: help with populating dropdownlist in datalist

    I don't see anything wrong with that code. What happens when you try to run it? Do you get an exception or just no data?

  3. #3
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: help with populating dropdownlist in datalist

    try:
    VB Code:
    1. With DropDownList1
    2.             .DataSource = sqlDR
    3.             .DataTextField = "place_name"
    4.             .DataValueField = "place_id"
    5.             .DataBind()
    6.         End With
    Woka

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2002
    Location
    Finland
    Posts
    15

    Resolved Re: help with populating dropdownlist in datalist

    Thanks for your help. The real problem was that i did get nothing showing in dropdownlist. Or then i get an error message (about object's instance).

    Finally i find solution from 4GuysFromRolla.com. Only thing i needed to do was to determine database connection and dataset to globally on the page. Here is the page that saved my day
    http://aspnet.4guysfromrolla.com/art...80702-1.2.aspx

    -Minna

  5. #5
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: help with populating dropdownlist in datalist

    That should not make any difference what so ever.
    Doing:
    VB Code:
    1. Private Const CONN_STRING As String = "Blah blah blah"
    2.  
    3. Sub BindData(ByVal CompanyID As Integer)
    4.        
    5.         Dim SQL As String = "SELECT * FROM Woof WHERE ID=" & CompanyID.ToString
    6.         Dim Conn As New SQLConnection(CONN_STRING)
    7.         Dim Comm As New SQLCommand(SQL, Conn)
    8.         Dim da As New SQLDataAdapter(Comm)
    9.  
    10.         Dim dt As New DataTable
    11.  
    12.         da.Fill(dt)
    13.  
    14.         With DropDownList1
    15.             .DataSource = dt
    16.             .DataTextField = "place_name"
    17.             .DataValueField = "place_id"
    18.             .DataBind()
    19.         End With
    20. End Sub
    Would work perfectly.

    Wpoka

  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2002
    Location
    Finland
    Posts
    15

    Re: help with populating dropdownlist in datalist

    i tried that and it worked when button was clicked two times. maybe it had something to do with the datalist. i had a table in the page and inside of it was datalist with itemtemplate and edittemplate. in itemtemplate i had a list of places and "upate"-button. when update-button was clicked, it should have shown places in the dropdownlist. the dropdownlist was empty, but when executing the same function (that should populate the dropdownlist) second time, then it worked. so, i'm guessing that i just did not know how to work with item- ja edittemplates.

    anyway, thanks.

    have a nice weekend

    -Minna

  7. #7
    Junior Member
    Join Date
    Oct 2006
    Posts
    22

    Re: help with populating dropdownlist in datalist

    this is what I was looking for as well, it worked a treat, thanks guys

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width