ASP.net C# - Dropdown List

  • I am working to get a drop down to work in a DetailsView with the intent to show the current value from a dataset, along with other additional values so that users can edit the data.  In this example, this is a for State drop down.  If my current state is DC, than I want DC to display with all other states following in the drop down.  However, I cannot seem to get this to work without errors.

    What this does is get the drop of state to select from, but I am unable to display the current value as default.

    <asp:TemplateField HeaderText="State" SortExpression="">

    <EditItemTemplate>

    <asp:DropDownList ID="ddlState" runat="server" DataSourceID="dsState" DataTextField="Abbrv" DataValueField="Abbrv" AppendDataBoundItems = "true">

    </asp:DropDownList>

    <asp:SqlDataSource ID="dsState" runat="server" ConnectionString="<%$ ConnectionStrings:T2DKLR %>" SelectCommand="SELECT [StateID], [Abbrv] FROM [State] ORDER BY [Abbrv]"></asp:SqlDataSource>

    </EditItemTemplate>

    </asp:TemplateField>

    Many thanks in advance for any insights.

     

     

  • Thanks for posting your issue and hopefully someone will answer soon.

    This is an automated bump to increase visibility of your question.

  • My opinion, this type of question is usually better asked on web-based development forums.  The database has no idea what field is selected by default at the application side based on your query that you are sending to the database.  Now, if you pass the selected value to the database, you could do that with a UNION ALL statement (to prevent sorting).  1st query would grab JUST the selected value and the UNION'ed query would grab all except the selected value.

    Personally, I would not handle this on the SQL side.  I would let SQL grab all of the data for you and let your application side handle the "default" value.  I would also look at doing this inside your .cs file not inside the aspx file.  There are a LOT of tutorials on how to do this such as https://parallelcodes.com/asp-net-fill-dropdownlist-ms-sql-server-database/.  I like that one because it provides details from having nothing (not even the database side set up) to having everything.  Plus it has the video and the code you can copy-paste.

    My opinion though - you will get more results if you are posting questions on the proper forums.  SQL Server Central is going to be helpful with SQL Server related questions, and may not have good answers to web design.

     

    The above is all just my opinion on what you should do. 
    As with all advice you find on a random internet forum - you shouldn't blindly follow it.  Always test on a test server to see if there is negative side effects before making changes to live!
    I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.

  • This was removed by the editor as SPAM

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply