Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET VB.NET
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.
 
' 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 13-Mar-12 22:57pm
Edited 13-Mar-12 23:03pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Dim Li2 As New ListItem()
      Li2.Text = "ALL"
      Li2.Value = ""  
      DDLModality.Items.Add(Li2)
  Permalink  
Comments
dholakiya ankit at 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+++++
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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)
 
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
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
 
Li1 = new ListItem();
Li1.Text = "ALL";
Li1.Value = ""
DDLModality.Items.Add(Li1)
 

 
Hope this helps you.
  Permalink  
Comments
dholakiya ankit at 13-Sep-13 6:23am
   
nice
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.
  Permalink  
v2
Comments
CHill60 at 3-Jul-13 12:40pm
   
I hope it helps too ... but the question was asked over a year ago

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 341
1 Marcin Kozub 225
2 Praneet Nadkar 197
3 Sergey Alexandrovich Kryukov 190
4 Shweta N Mishra 161
0 OriginalGriff 8,149
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 3 Jul 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100