Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot have multiple items selected in a DropDownList. 1

Status
Not open for further replies.

stonehead

Technical User
May 2, 2007
58
US
All,
Below is what I have and when I click submit button I got the error "Cannot have multiple items selected in a DropDownList" Any help is appreciated.

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

If Not Page.IsPostBack Then

Session("myTable") = New Data.DataView()

ddApp.Items.Add(New ListItem("--- Please Select ---"))
ddApp.Items.Add(New ListItem("ABC"))
ddApp.Items.Add(New ListItem("DEF"))
ddApp.Items.Add(New ListItem("GHI"))

Dim i As Integer
For i = 1 To 10
ddNum.Items.Add(New ListItem(i))
ddNum.SelectedIndex = 0
Next i
End If

End Sub

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
cmdInsert.Parameters.AddWithValue("@name", ddApp.SelectedItem.Text)
cmdInsert.Parameters.AddWithValue("@copy_num", ddNum.SelectedItem.Text)

end sub
 
On page_load your DDL will reload its items each time from the viewstate. In your code you are setting an item to be selected. On post back, your items reload and have an item selected from the user, but then you try to selected another item in code.
You have 2 choices, you can turn off viewstate for your DDL and your code should be fine, or, in your page load code, clear the DDL itmes before your reload them.
 
i wouldn't think this would cause an issue, but move
Code:
ddNum.SelectedIndex = 0
outside for for loop.

if you have a static list of options, like the 2 you have above, it's much simpler to simply hard code the values in the markup.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Good point Jason. I overlooked that that was being set in the loop.
 
Thanks so much for your prompt response. I set viewstate = false (or do ddNum.Items.Clear()) and got error "System.NullReferenceException: Object reference not set to an instance of an object."

cmdInsert.Parameters.AddWithValue("@name", ddApp.SelectedItem.Text)

Any idea? Thank you.
 
Thank you. I already moved it out of for loop but still same error.
 
Since I don't have all of your code, I would take Jason's suggestion and put the items in the HTML markup and remove it from the code-behind.
 
I've never done the html markup before. Could you please give me more details. Sample codes are appreciated. Thanks.
 
Never mind. I found out why I had that error. It's because after submit I set the dropdownlists back to default by
ddApp.item(0).selected = true

by changing it to
ddMap.SelectedIndex = 0
it works just fine.

Thank you all for your help
 
Great point by Mark. Also in the future, post all relevant code. Without it, we are just guessing what the problem could be.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top