Skip to content

Bind data to a DropDownList in Edit & Insert in ListView

Terry

Asked Mar 26, 2015, 6:18 AM EDT1 answer
Share
LinkedInX
Hi, 

    In my ASP.NET 4.5 WebForms app, In my ListView I am trying to bind DropDownList, but am not able to do it. Actually, the Model that is bind to ListView, has a Property of UnitdirectionId & on Edit & Insert Templates' I want to show the dropdown of all UnitDirection table.

Here is my EditTempalte :
           
               
                   
                       
                       
                       
                           DataTextField="DirectionTitle" DataValueField="UnitDirectionId" 
                           SelectMethod="GetUnitDirections">  
                    
             

           


In Code behind, 
Code
        public IQueryable GetUnitDirections()
        {
            return _db.UnitDirections;
        }

Now, the Model of LsitView has the proerpty as follows :
        [Display(Name= "Unit Direction")]
        public int UnitDirectionId { get; set; }

        [ForeignKey("UnitDirectionId")]
        public virtual UnitDirection UnitDirection { get; set; }

Now, the UnitDirection Model :
C#
    public class UnitDirection
    {
        [ScaffoldColumn(false)]
        public int UnitDirectionId { get; set; }

        [Required]
        [Display]
        public string DirectionTitle { get; set; }
    }

In db the UnitDirection table has 3 items. 

With the  above drop down, I believe I should atleast get list of all UnitDirection in drop down. But on that I am getting error. Then comes to set the selected item in Edit Template.

Can you please help me identify why I am getting this error ?? 
Any help is highly appreciated.

Thanks




1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    I have solved this question.
    Sharing my answer, so it can be helpful to others facing similar problem.


    The solution was in webconfig ConnectionString. I added MultipleActiveResultSets :
    Integrated Security=True;MultipleActiveResultSets=true

    in the connectionString and things worked.

    The issue was, DB was already engaged in retrieving/accessing data in Edit/Insert model, hence it wasn't allowing to run other query while it was busy in an already existing resultset.
    0