How to fill dropdown list in asp.net from DB table

I am new to asp.net and I want to populate a dropdown list with values โ€‹โ€‹from the created table. I only want to populate the list with one of the fields - languages โ€‹โ€‹in my table. I think I connected to the data source correctly, but I don't know what I need to do to get the values โ€‹โ€‹in the list. I can enter my own values, but I would prefer it to be automated.

This is what I have so far, but I'm guessing there is more to it than just binding the list to a data source. Any help would be greatly appreciated.

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:HBshareIndexConnectionString %>" 

SelectCommand="SELECT * FROM [Web_Metrics] WHERE ([LCID] = @LCID)">
<SelectParameters>
    <asp:QueryStringParameter Name="LCID" QueryStringField="LCID" Type="Int32" />
</SelectParameters>

</asp:SqlDataSource>

<asp:Label ID="Label1" runat="server" Text="Select LCID: " ></asp:Label> &nbsp; &nbsp;  
<asp:DropDownList ID="DropDownList1" Width="150px" runat="server" DataSourceID="SqlDataSource1"  DataTextField="LCID" DataValueField="LCID">
<asp:ListItem>Select LCID...</asp:ListItem>
</asp:DropDownList>

      

Hi guys, thanks for the help. I got the dropdown filled in now, but I was wondering how can I get the repeater I am using to display the details of the LCID that the person picks? I've seen people talk about page.isPostback, but I don't know what it is or is working with my current setup. I need to somehow get the selected LCID and then refresh the page to show the details of that LCID. Does anyone have any idea? Thanks to

+3


source to share


2 answers


Your problem is that you are trying to define list items and data source.

If you want to insert a Select Item option, I would suggest adding it to your result set (to always be first with UNION

and ORDER BY

can be tricky depending on your fields), or inserting it after the data binding in your code behind:

Changing attributes DropDownList1

:

<asp:DropDownList ID="DropDownList1" Width="150px" runat="server" DataSourceID="SqlDataSource1"  DataTextField="CountryName" DataValueField="LCID" OnDataBound="InsertChooseItem" />

      



FROM#:

protected void InsertChooseItem(object sender, EventArgs e)
{
    ListItem selectOnePlease = new ListItem("Select LCID..", 0);

    DropDownList1.Items.Insert(0, selectOnePlease);
}

      

VB:

Protected Sub InsertChooseItem(sender As Object, e As EventArgs)

    Dim selectOnePlease As New ListItem("Select LCID..", 0)

    DropDownList1.Items.Insert(0, selectOnePlease)

End Sub

      

+1


source


You have specified that the select parameter is a query string so the data in the DropDownList will only be populated when the url looks like:

http://{Your server name}/Default.aspx?LCID=1

      

It doesn't make any sense, because the LCID column in your table must be unique, so while this will work, there will only be one value in the dropdown.

I think you want to display all languages โ€‹โ€‹from the database in a dropdown, here is an example:



<asp:SqlDataSource ID="sqlDS" runat="server" 
ConnectionString="<%$ ConnectionStrings:HBshareIndexConnectionString %>"
    SelectCommand="SELECT LCID,Language FROM [Web_Metrics]">
</asp:SqlDataSource>
<asp:DropDownList ID="ddlLanguages" AppendDataBoundItems="true" Width="150px" runat="server" DataSourceID="sqlDS" DataTextField="Language" DataValueField="LCID">
    <asp:ListItem>Select Language</asp:ListItem>
</asp:DropDownList>

      

Just note, you should never show the ID to the client, it is completely pointless for them, IDs are mostly used by developers in the background, so in the dropdown I set:

  • DataTextField = "Language" (this is the user-visible language name)
  • DataValueField = "LCID" (not visible to the user, but useful for any additional processing in the code)

    AppendDataBoundItems = "true" - This line of code will keep all the items you manually added to the dropdown, such as Select Language, and add any data related items, such as from a SQL table

0


source







All Articles