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:
<asp:DropDownList ID="dropList" Runat="server" DataTextField="place_name" DataValueField="place_id"></asp:DropDownList>
and here is what i have tried:
VB Code:
Sub BindData(ByVal intCompany_id As Integer)
Dim sqlConnStr As String = ConfigurationSettings.AppSettings("ConnectionString")
Dim sqlConn As New SqlConnection(sqlConnStr)
Dim sqlSelect As String = "SELECT * FROM Places WHERE place_company_id=" & intCompany_id
Dim sqlCmd As SqlCommand
sqlConn.Open()
Dim sqlDR As SqlDataReader
sqlCmd = New SqlCommand(sqlSelect, sqlConn)
sqlDR = sqlCmd.ExecuteReader()
While sqlDR.Read()
Dim newListItem As New ListItem
newListItem.Text = sqlDR.Item("place_name")
newListItem.Value = sqlDR.Item("place_id")
dropList.Items.Add(newListItem)
End While
sqlConn.Close()
End Sub
I don't know how to catch the right dropdownlist object in datalist to populate. any ideas? anyone?
Thanks in advance
- Minna :wave:
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?
Re: help with populating dropdownlist in datalist
try:
VB Code:
With DropDownList1
.DataSource = sqlDR
.DataTextField = "place_name"
.DataValueField = "place_id"
.DataBind()
End With
Woka
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 :D
http://aspnet.4guysfromrolla.com/art...80702-1.2.aspx
-Minna
Re: help with populating dropdownlist in datalist
That should not make any difference what so ever.
Doing:
VB Code:
Private Const CONN_STRING As String = "Blah blah blah"
Sub BindData(ByVal CompanyID As Integer)
Dim SQL As String = "SELECT * FROM Woof WHERE ID=" & CompanyID.ToString
Dim Conn As New SQLConnection(CONN_STRING)
Dim Comm As New SQLCommand(SQL, Conn)
Dim da As New SQLDataAdapter(Comm)
Dim dt As New DataTable
da.Fill(dt)
With DropDownList1
.DataSource = dt
.DataTextField = "place_name"
.DataValueField = "place_id"
.DataBind()
End With
End Sub
Would work perfectly.
Wpoka
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 :wave:
-Minna
Re: help with populating dropdownlist in datalist
this is what I was looking for as well, it worked a treat, thanks guys :)