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

I am trying to fill an <asp:DropDownList> with data from a database but i cant seem to get it to work as nothing shows. I am not sure if i am completely missing the point or whther i am just not linking the dropdown with the data correctly.


To make things clear i have a div on one page which shows a table with some data and the dropdownlist with two buttons. The data to fill the table comes from a stealth page that is not seen i only use it to create the table and then on the page the
is located i use JavaScript to call the table data.(sorry if this doesn't make sense)

i need to use the same method for the dropdown or even a diferent way to populate the dropdown but i cant seem to do this.

My Code..

yhis is my div located on my search page
XML
<div id="dUserSystems" runat="server" >
        <u>Systems user has access to</u><br />
            <div id="dUserData"></div>
        <br /><br />
    <div id="ddlSysData" runat="server" >
         <u>Add new system to users account</u><br />

        <asp:DropDownList ID="sysDDL" runat="server"></asp:DropDownList>

    </div>
        <br /><br />
        <input type="button" id="butAdd" value="Add" runat="server" style="font-family: Verdana,Tahoma,Arial; float: left;"/>&nbsp;&nbsp;
        <input type="button" onclick="butClose()" value="Close" />
</div>


below is my server side code creating the data for the table from a different page

VB
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        'Add session check here

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

            Dim dsResults As New DataSet
            Dim dt As DataTableReader

            dsResults = GetUserName.systemData(Request.QueryString("userid").ToString)
            dt = dsResults.CreateDataReader

            Dim dv As New HtmlGenericControl("div")
            dv.ID = "sysData"

            Dim tb As New Table
            Dim tr As New TableRow
            Dim tc As New TableCell
            Dim lb As New Label
            Dim iCount As Integer = 1

            If dt.HasRows = True Then

                tr = New TableRow
                tr.CssClass = "tabHeader"
                tc = New TableCell
                tc.CssClass = "tabCell"
                lb = New Label

                lb.Text = "System Name"
                tc.Controls.Add(lb)
                tr.Controls.Add(tc)
                tb.Controls.Add(tr)

                tr.CssClass = "tabHeader"
                tc = New TableCell
                tc.CssClass = "tabCell"
                lb = New Label

                lb.Text = "Date Added"
                tc.Controls.Add(lb)
                tr.Controls.Add(tc)
                tb.Controls.Add(tr)


                tr.CssClass = "tabHeader"
                tc = New TableCell
                tc.CssClass = "tabCell"
                lb = New Label

                lb.Text = "Added By"
                tc.Controls.Add(lb)
                tr.Controls.Add(tc)
                tb.Controls.Add(tr)

                Do While dt.Read

                    tr = New TableRow

                    If iCount = 1 Then
                        tr.CssClass = "rowStyle3"
                        iCount = 0
                    Else
                        tr.CssClass = "rowStyle4"
                        iCount = 1
                    End If

                    'tr = New TableRow
                    tc = New TableCell
                    tc.CssClass = "rowCell2"
                    lb = New Label

                    lb.Text = dt.Item("description").ToString()
                    tc.Controls.Add(lb)
                    tr.Controls.Add(tc)
                    tb.Controls.Add(tr)

                    'tr = New TableRow
                    tc = New TableCell
                    tc.CssClass = "rowCell2"
                    lb = New Label


                    lb.Text = dt.Item("date_added").ToString()
                    tc.Controls.Add(lb)
                    tr.Controls.Add(tc)
                    tb.Controls.Add(tr)

                    'tr = New TableRow
                    tc = New TableCell
                    tc.CssClass = "rowCell2"
                    lb = New Label

                    lb.Text = dt.Item("added_by").ToString()
                    tc.Controls.Add(lb)
                    tr.Controls.Add(tc)
                    tb.Controls.Add(tr)
                Loop
                dt.Close()
                dsResults.Dispose()


                dv.Controls.Add(tb)
                sysData.Controls.Add(dv)
            Else
                Response.Write("No Systems Located for User")
            End If


        End If


I have tried creating a table with the data as above for the drop down but i have had no luck.

If i have confused or you need more info please let me know.

Thank You


**Updated Info**

I am listing some further info that may help with my question.

1. I have a button on a search page that when pressed shows the data as a styled popup that is created on the search page.

2. The data that is within the div is called from another page(fetchData.aspx) that i am creating the table on as shown above.

3. On the (fetchData.aspx) page i have
VB
<asp:PlaceHolder ID="sysData" runat="server"></asp:PlaceHolder> 


Full
on search page..

VB
<div id="dUserSystems" runat="server" >
            <u>Systems user has access to</u><br />
                <div id="dUserData"></div>
            <br /><br />
        <div id="ddlSysData" runat="server" >
             <u>Add new system to users account</u><br />

            <asp:DropDownList ID="sysDDL" runat="server"></asp:DropDownList>

        </div>
            <br /><br />
            <input type="button" id="butAdd" value="Add" runat="server"/>&nbsp;
            <input type="button" onclick="butClose()" value="Close" />
    </div>


Posted
Updated 20-Jan-15 0:37am
v2

1 solution

This should set your datasource. See if it goes well with autobinding. Then you can play with properties.

VB
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        'Add session check here

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

            Dim dsResults As New DataSet
            Dim dt As DataTableReader

            dsResults = GetUserName.systemData(Request.QueryString("userid").ToString)
        sysDll.DataSource = dsResults.Tables(0) 
End Sub



NOTE: Drop down list has only two values DataTextField (i.e. description) and DataValueField (i.e. id) . What you're trying to do is probably calling for DataList, DataGridView or asp:Repeater where you can bind multiple columns.

What you're currently doing IS missing the point. You're using list control and then create the table to show inside it. If you want that format, you don't have to use list control at all, just append your table to any div.

List controls will generate their own grids without you having to do manual work. You set the columns, assign datasource and let the framework / control work. Then you tweak styles until you're satisfied.
 
Share this answer
 
v2
Comments
Member 11355710 20-Jan-15 6:11am    
Thank you this looks good the problem is i cant use sysDDL.DataSource because the DDL is not the page i am calling the data so it keeps telling me sysDDL is not declared. I think i have missed some info out i will add this to the bottom of my Question.
Sinisa Hajnal 20-Jan-15 6:31am    
Erm...what?! You'll have to explain what you're calling from where and why. If you're not binding the control on the page you're on, what are you trying to do?

Ah, I see...no, you shouldn't create the table like that. Instead create web method and call the data retrieving function). Or better yet, create the method in your business logic that returns datatable and call it from wherever you need it (in this case, in your page load event)
Member 11355710 20-Jan-15 6:39am    
I am new to vb.net and still learning a great deal, i wouldn't know where to start in terms of " create web method and call the data retrieving function" or "create the method in your business logic that returns datatable and call it from wherever you need it"
Sinisa Hajnal 20-Jan-15 9:46am    
Data retrieving function is the one you're already (GetUserName.systemData(Request.QueryString("userid").ToString))

WebMethod is attribute on the method that marks it as available to other pages (commonly for AJAX calls)

Business logic layer is the one where you should keep your validations, requests for data (calls to data layer) etc...

As with everything in programming, once you hear certain term, you google it and then spend couple of hours reading about it to get the feel what is possible (hint: everything is possible, except you cannot make the flowers grow OUT of the monitor :) )

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