PDA

Click to See Complete Forum and Search --> : help with populating dropdownlist in datalist


arctic_bug
Sep 28th, 2005, 02:44 AM
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:

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

and here is what i have tried:


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:

dogboydelta
Sep 28th, 2005, 09:53 AM
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?

Wokawidget
Sep 28th, 2005, 10:52 AM
try:

With DropDownList1
.DataSource = sqlDR
.DataTextField = "place_name"
.DataValueField = "place_id"
.DataBind()
End With

Woka

arctic_bug
Sep 29th, 2005, 06:12 AM
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/articles/080702-1.2.aspx

-Minna

Wokawidget
Sep 29th, 2005, 06:23 AM
That should not make any difference what so ever.
Doing:

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

arctic_bug
Sep 30th, 2005, 08:14 AM
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

Ping_Chow_Chi
Oct 22nd, 2008, 05:25 AM
this is what I was looking for as well, it worked a treat, thanks guys :)