Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

I am in need of some help with constructing a drop down list within vb.net server side.

I have found i can start with something like
VB
Dim dl As New DropDownList
but iam stuck as of how to continue, i have search online for help but it all looks confusing.

I need to populate the DropDownList with data from a stored procedure

if you need anymore code or info please ask

Thank you

I have tried filling in the DDL in with the following code but nothing is populated

VB
If Request.QueryString("userid") <> "" Then

            Dim ddlResult As New DataSet
            ddlResult = GetUserName.ddlData(Request.QueryString("userid").ToString)

            sysDDL.DataSource = ddlResult
            sysDDL.DataValueField = "description"
            sysDDL.DataTextField = "description"
            sysDDL.DataBind()
            sysDDL.Items.Insert(0, New ListItem(String.Empty, String.Empty))
            sysDDL.SelectedIndex = 0

        End If


New Code...

DIV containing DDL
XML
<div id="dUserSystems" runat="server" >
        <u>Systems user has access to</u><br />
            <div id="dUserData"></div>
        <br />

    <div id="ddlSysData" runat="server" >
         <u>Add new system to users account</u><br />
         <asp:DropDownList ID="sysDDL" runat="server"/>
    </div>
        <br />
        <input type="button" id="butAdd" value="Add" runat="server"/>&nbsp;
        <input type="button" onclick="butClose()" value="Close" />
</div>



My function that calls data from a stealth page to create a table for 'dUserData' and i think i may need to add something in here for the DDL to work?

JavaScript
function viewSystems(myUserid) {


       var link = 'fetchData.aspx?userid=' + myUserid;

       $.get(link, function (data) {

           $("#dUserData").empty().append(data);
           $("#ContentPlaceHolder1_dUserSystems").css("left", "200px");
           $("#ContentPlaceHolder1_dUserSystems").css("display", "block");
       });


     }

   function butClose() {
       $("#ContentPlaceHolder1_dUserSystems").css("display", "none");
   }
Posted
Updated 21-Jan-15 3:57am
v6

1. Get the data in DataSet or DataTable (or list, collection etc)
2. set DataTextField and DataValueField - first is column name that is shown to the user, second one is the value you get for dl.SelectedValue
3. Set dl.DataSource to your data (whatever that is)

Read detailed description here[^] - it wasn't hard to find and it is clear, step-by-step with code examples and download.

Put some more effort into it next time.

Code:
You don't need New DropDownList, if you do it this way you have to add it to the page somewhere. Instead, put
ASP.NET
<asp:DropDownList id="dl" runat="server" />
where you need it in your html.

Server side:
VB
If Request.QueryString("userid").Trim <> String.Empty Then

            Dim ddlResult As New DataSet
            ddlResult = GetUserName.ddlData(Request.QueryString("userid").ToString)

        dim dr as DataRow = ddlResult.Tables(0).NewRow
        dr("description") = string.Empty
        ddlResult.Tables(0).Rows.InsertAt(dr, 0)
        ddlResult.AcceptChanges()

            sysDDL.DataValueField = "description"
            sysDDL.DataTextField = "description"
            sysDDL.DataSource = ddlResult.Tables(0)
            sysDDL.DataBind()
        End If
 
Share this answer
 
v2
Comments
Member 11355710 21-Jan-15 7:27am    
Thank you Sinisa but i am new to vb.net and i have not created a DDL this way before so i am confused of what i need. You list makes sense but i am unsure of how to turn this into code
Sinisa Hajnal 21-Jan-15 7:31am    
That is why I supplied you with the link where it is described in detail. Being new does not excuse you from searching and learning. I realize it is hard without someone senior next to you, but persistence pays. And once you "get it" you'll never have to ask again for all types of similar controls. So...keep reading, check the link, download and understand the code...and you'll be the one answering the questions in ... decade or so :p
Member 11355710 21-Jan-15 7:42am    
Thank you for your comments but i really needed some help with a starting point of code as after reading the link provided before asking this question it doesnt help me with my issue. I am using a Stored Procedure and a QueryString before i construct the DDL. I completely agree with researching and learning but after reading 20/30 pages none of them have a simple DDL construct im looking for so i thought i would try asking the question.
Sinisa Hajnal 21-Jan-15 9:23am    
But the point is: you already have your data in form of GetUserName.ddlData(Request.QueryString("userid").ToString)

So, assign data column names to properties mentioned above and assign datasource. That really is all. The controls are actually very simple. You can get into tweaks later, but the initial simple binding is exactly that, simple.

I apologize for the comment about the effort put into this, but there are lots of exact code examples - you have to only copy and paste them, change column names and you're done.

The above IS starting point. What you're asking is for finished code. I'll edit the solution, but this really shouldn't be neccessary.
Sinisa Hajnal 21-Jan-15 9:28am    
There. Check the solution.
OK. Remove your "stealth" page. Add the code for data retrieval in your page. You can later play with moving it around. I believe there was a suggestion of doing refactoring earlier...

So...instead of having hidden page to do your data retrieval, create data management class that you can reference and call its methods. It doesn't have to be a page.

In AppCode folder add:
VB
public class UserManager
public function GetUser(id as string) as datatable
' whatever you need to do to return the table
end function
End Class


in your page:
VB
dim um as new UserManager()
dim dt as datatable = um.GetUser(QueryString("id"))
...the rest of the code to assign dt as datasource to your dropdown.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900