Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 vb.net: Populating drop down from recordset

Author  Topic 

ninel
Posting Yak Master

141 Posts

Posted - 2005-06-29 : 12:25:46
I have a stored procedure that returns a recordset containing project names. I need to populate a drop down with this recordset.


[CODE]
CREATE PROCEDURE dbo.uspGetProjects
AS
BEGIN
SELECT sName
FROM msProjects
END
[/code]

DropDown Code:
[CODE]
<HTML>
<HEAD>
<title>Projects</title>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server"><asp:DropDownList id="ddlProject" style="Z-INDEX: 101; LEFT: 456px; POSITION: absolute; TOP: 152px" runat="server" Width="160px"></asp:DropDownList>
</form>
</body>
</HTML>
[/code]

How do I do this with vb.net?

Thanks,
Ninel

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2005-06-29 : 14:36:12
Put the results of the stored procedure into a data table. Then:

ddlProject.DataSource = DataTableName

Tara
Go to Top of Page

MarkGG
Yak Posting Veteran

53 Posts

Posted - 2005-07-12 : 13:54:51
Sorry I don't have the time to specifically apply it to your case, but here is something from one of my pages I have:

quote:

<center>
<table border = "2" bgcolor="white">
<th>Township</th>
<%
SQL = "Select township from township order by township"
objRS.Open SQL, dbConnect
Do While Not objRS.EOF
%>
<tr>
<td><a href="TownshipDel.asp?township=<%=objRS("township")%>"><input type="text" readonly size="25" name="<%=objRS("township")%>" value="<%=objRS("township")%>"></a></td>
</tr>

<%
objRS.movenext
Loop
objRS.close
con.close
Set con = Nothing
%>
</table>
</center>
Go to Top of Page

twhelan1
Yak Posting Veteran

71 Posts

Posted - 2005-07-12 : 15:14:09
Ack! No, he's talking ASP.Net, that's old school ASP...

It's simple, execute the stored procedure using ADO.Net into a DataSet, then as Tara stated use the following line:

ddlProject.DataSource = DataTableName

though that's just the beginning. After that you'll need:

ddlProject.DataBind()

And in the properties of the DropDownList you'll need to set the DataValueMember and DataTextMember otherwise you'll get garbage as values in your DropDownList.
Go to Top of Page

TheBluePhoenix
Starting Member

4 Posts

Posted - 2005-07-22 : 15:02:08
This is how I do it:

Dim dtr_Clients As SqlDataReader
dtr_Clients = obj_Clients.SelectAllForDropdown
If dtr_Clients.HasRows Then
drp_Clients.DataSource = dtr_Clients
drp_Clients.DataTextField = "ClientName"
drp_Clients.DataValueField = "ClientNames_ID"
drp_Clients.DataBind
End If

SelectAllForDropdown is a method that queries the db and returns a recordset.

After you bind the data to the dropdown, you can still add items to the dropdown like so:

drp_Clients.Items.Insert(0, "[Select One]")

or

drp_Clients.Items.Insert(0, new ListItem("[Select One]", ""))


Move like you have a purpose.
Go to Top of Page
   

- Advertisement -