• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 298
  • Last Modified:

Databound DDL not keeping all list items

I have a ddl, which is not initially bound to a datasource with 4 listItems. User selects one list item, then clicks the save button, which inputs the selected item into database. When I recall the selected item back to the ddl on page load, I have the ddl databound, and it recalls the item that was selected just fine, but it does not show the other list items in the ddl. I need to have all the items still available, in case the user needs to change their selection.

Here is my code:

Script:

Dim Adapter As New SqlDataAdapter("SELECT * FROM Orders Where OrdersID = " & list1.SelectedItem.Value, "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Client.mdf;Integrated Security=True;User Instance=True")

Dim Dataset As New DataSet

Adapter.Fill(Dataset, "Orders")

Me.Representing.DataMember = "Orders"

Me.Representing.DataSource = Dataset

Me.Representing.DataTextField = "Representing"

Me.Representing.DataValueField = "OrdersID"

Me.Representing.DataBind()

 Content:

<asp:DropDownList ID="Representing" runat="server" Height="18px"

Font-Size="X-Small" Width="129px">

<asp:ListItem Value="Plaintiff">Plaintiff</asp:ListItem>

<asp:ListItem Value="Defendant">Defendant</asp:ListItem>

<asp:ListItem Value="Petitioner">Petitioner</asp:ListItem>

<asp:ListItem Value="Respondent">Respondent</asp:ListItem>

</asp:DropDownList>
0
mlg101
Asked:
mlg101
1 Solution
 
Solar_FlareCommented:
From your description I would say that you should not be using Databinding for this - what happens is that when you bind the DDL all existing items are cleared and new items are added based on the bound object. If you change it so that instead of databinding you set the selectedText property then it should leave the list intact but change the selected item.

eg

Dim Adapter As New SqlDataAdapter("SELECT * FROM Orders Where OrdersID = " & list1.SelectedItem.Value, "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Client.mdf;Integrated Security=True;User Instance=True")

Dim Dataset As New DataSet

Adapter.Fill(Dataset, "Orders")

dim SelectedVal as String = Dataset.Tables(0).Rows(0)("Representing").ToString()

Representing.SelectedText = SelectedVal


you may need to change SelectedText to SelectedValue depending on how you are using the DDL
0
 
mlg101Author Commented:
Thank you! Worked great!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now