Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my code and i am getting an error ..Cannot have multiple items selected in a DropDownList. when the page loads it select "ALL" in DDLModality but when i change DDLModality.selectedvalue to any "value" then no error but again when i change to "ALL "getting the error.

VB
' USED TO ADD REFERRING PHYSICIAN IN THE DROPDOWN DYNAMICALLY 
    If CInt(Session("CenterID")) = 0 Then
        sql = "Select Ref_Phy_ID,Name from Ref_Phy_Master WHERE Ref_Phy_ID in(Select distinct Ref_Phy_ID from Patient_Details where Ref_Phy_ID <> '')"
    Else
        sql = "Select Ref_Phy_ID,Name from Ref_Phy_Master WHERE Ref_Phy_ID in(Select distinct Ref_Phy_ID from Patient_Details where Ref_Phy_ID <> '') And Center_ID = " & Session("CenterID")
    End If
    objDS = objFun.RunQuery(sql)
    ' USED TO REFRESH THE PAGE WHIN IT IS POSTED BACK 
    ' USED TO DISPLAY DEFAULT FIRST ITEM IN THE DROPDOWN 
    Dim Li1 As New ListItem()
    Li1.Text = "ALL"
    Li1.Value = ""
    cboRefPhy.Items.Add(Li1)
    ' USED TO COUNT THE STUDIES IN THE DROPDOWN 
    If (objDS.Tables(0).Rows.Count <> 0) Then
        ' USED TO CIRCULATE LOOP UPTO THE RECORD COUNT
        Dim i As Integer
        For i = 0 To objDS.Tables(0).Rows.Count - 1
            ' USED TO CREATE NEW ITEM IN THE DROPDOWN 
            Dim Li As New ListItem
            Li.Text = objDS.Tables(0).Rows(i)("Name").ToString()
            Li.Value = objDS.Tables(0).Rows(i)("Ref_Phy_ID").ToString()
            'USED TO ADD ITEMS IN THE DROPDOWN 
            cboRefPhy.Items.Add(Li)
        Next
    End If
    'USED TO SAVE THE CHANGES IN DATASET  
    objDS.AcceptChanges()
    ' USED TO CLOSE THE DATABASE CONNECTION 
    objDS.Dispose()
    cboRefPhy.ClearSelection()
    cboRefPhy.SelectedValue = Convert.ToString(Request.Form("cboRefPhy"))

    'USED TO ADD MODALITY IN THE DROPDOWN DYNAMICALLY
    If CInt(Session("CenterID")) = 0 Then
        sqlStudy = "Select Modality_ID,Modality from Hospital_Modality_Master WHERE Modality_ID in(Select distinct Study_ID from Patient_Details where Study_ID <> '')"
    Else
        sqlStudy = "Select Modality_ID,Modality from Hospital_Modality_Master WHERE Modality_ID in(Select distinct Study_ID from Patient_Details where Study_ID <> '') And Center_ID = " & Session("CenterID")
    End If
    'Dim objDS As New DataSet()
    objDS = objFun.RunQuery(sqlStudy)
    ' USED TO REFRESH THE PAGE WHIN IT IS POSTED BACK 
    ' USED TO DISPLAY DEFAULT FIRST ITEM IN THE DROPDOWN 
    'Dim Li1 As New ListItem()
    Li1.Text = "ALL"
    Li1.Value = ""
    ' Dim all As String
    ' all = "All"
    'Ddl_Modality.Items.Add(all)
    DDLModality.Items.Add(Li1)
    ' USED TO COUNT THE STUDIES IN THE DROPDOWN 
    If (objDS.Tables(0).Rows.Count <> 0) Then
        ' USED TO CIRCULATE LOOP UPTO THE RECORD COUNT
        Dim i As Integer
        For i = 0 To objDS.Tables(0).Rows.Count - 1
            ' USED TO CREATE NEW ITEM IN THE DROPDOWN 
            Dim Li As New ListItem
            Li.Text = objDS.Tables(0).Rows(i)("Modality").ToString()
            Li.Value = objDS.Tables(0).Rows(i)("Modality_ID").ToString()
            'USED TO ADD ITEMS IN THE DROPDOWN 
            DDLModality.Items.Add(Li)
        Next
    End If
    'USED TO SAVE THE CHANGES IN DATASET  
    objDS.AcceptChanges()
    ' USED TO CLOSE THE DATABASE CONNECTION 
    objDS.Dispose()
    DDLModality.ClearSelection()
    DDLModality.SelectedValue = Convert.ToString(Request.Form("DDLModality"))


    ' USED TO ADD STUDY IN THE DROPDOWN DYNAMICALLY 
    If CInt(Session("CenterID")) = 0 Then
        sqlStudy = "Select Study_ID,Study_Desc from Study_Master WHERE Study_ID in(Select distinct Study_ID from Patient_Details where Study_ID <> '')"
    Else
        sqlStudy = "Select Study_ID,Study_Desc from Study_Master WHERE Study_ID in(Select distinct Study_ID from Patient_Details where Study_ID <> '') And Center_ID = " & Session("CenterID")
    End If

    If (DDLModality.SelectedItem.Text <> "ALL") Then
        sqlStudy = sqlStudy & " AND Modality = '" & DDLModality.SelectedItem.Text & "'"

    End If
Posted
Updated 13-Mar-12 22:03pm
v2

 
Share this answer
 
v2
Dim Li2 As New ListItem()
Li2.Text = "ALL"
Li2.Value = ""
DDLModality.Items.Add(Li2)
 
Share this answer
 
Comments
Dholakiya Ankit 13-Sep-13 6:23am    
excellent solution i was about to post new question but i could not understand why ? this solution thanks my 5ed+++++
The very first thing is before selecting any value in dropDown we should first check if it is present or not (just to make sure and avoid the unexpected scenarios)

C#
if(ddlDropDown.Items.FindByValue("ValueToSearch")!=null)
{
     ddlDropDown.Items.FindByValue.Selected=true;
}



Now lets come to your error "Cannot have multiple items selected in a DropDownList"
This error generally comes when we try to pass selection criteria for which there may be two items in the dropdown.

Let's say if your dropdown have two items with the value "" and you have written following code
C#
DDLDropDown.SelectedValue = "";


then it will throw this exception.

the simplest way is to check your page source from browser and see what final items were there in your dropdown.

Also one more thing when adding a new listItem, if you want to use same variable then at least reinitialize it

C#
Li1 = new ListItem();
Li1.Text = "ALL";
Li1.Value = ""
DDLModality.Items.Add(Li1)




Hope this helps you.
 
Share this answer
 
Comments
Dholakiya Ankit 13-Sep-13 6:23am    
nice
When you are using .Text or FindByValue or FindByText to assign values, first you need to clear the selection (ex ddl.ClearSelection()) of dropdown list control and then you need to assign a new value. Hope this will help.
 
Share this answer
 
v2
Comments
CHill60 3-Jul-13 12:40pm    
I hope it helps too ... but the question was asked over a year ago
Leon Sandler 10-Jun-19 12:06pm    
It worked! Thanks

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