Suyog Patil
Suyog Patil

Reputation: 1

Bind DropdownList with List?

        var list1 = dbContext.TruckTypes.Where(s => s.Status == "Active").ToList();
        aTypeDropDownList.DataSource = list1;
        aTypeDropDownList.DataTextField = "Name";
        aTypeDropDownList.DataValueField = "Id";
        aTypeDropDownList.DataBind();

Error : Additional information: 'aTypeDropDownList' has a SelectedValue which is invalid because it does not exist in the list of items.

public partial class TruckType { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] public TruckType() { this.Requests = new HashSet(); this.Trucks = new HashSet(); }

    public int Id { get; set; }
    public string Name { get; set; }
    public double Height { get; set; }
    public double Width { get; set; }
    public int MaxCapacity { get; set; }
    public string ImagePath { get; set; }
    public double LPriceKM { get; set; }
    public double MPriceKM { get; set; }
    public string Status { get; set; }
    public string CreatedBy { get; set; }
    public System.DateTime CreatedOn { get; set; }
    public string UpdatedBy { get; set; }
    public Nullable<System.DateTime> UpdatedOn { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Request> Requests { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Truck> Trucks { get; set; }
    public virtual User User { get; set; }
}

Upvotes: 0

Views: 48

Answers (2)

Suyog Patil
Suyog Patil

Reputation: 1

finally i figure-out the problem. aTypeDropdownLis.text = string.Empty;

i have putted this line in the code. after removing it, the program working fine.

Upvotes: 0

Arief
Arief

Reputation: 6085

When doing SelectedValue on a DropDownList with database bound values, make sure the value you are setting is actually in the list.

Example, your list contains:

<select>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>

Then you do SelectedValue = "8", this is causing error because value 8 is not in the list.

I really hope you understand

Upvotes: 1

Related Questions