2

I am new to programming and currently working on a C# asp.net website that populates Telerik RadComboBoxes from a database on_load.

I have a form that has 15+ ComboBoxes and when I select values from those ComboBoxes those ComboBox selections must be used to search through a really large table in my database. A gridView will display the returned data.

I have used the same format of code throughout the rest of my project and it works perfectly, But when I select an item from my "Location" DropDownBox to search through my database I get the error 'Input string was not in a correct format' and I cant figure out why

here is my Location.cs Class

region Properties

    [Key]
    public int LocationID { get; se; }
    [Column("Location")]
    public string LocationName { get; set; }
    private int? _ParentLocationID;
    [Column]
    public int? ParentLocationID
    {
        get
        {
            return _ParentLocationID;
        }
        set
        {
            if (value == 0)
            {
                _ParentLocationID = null;
            }
            else
            {
                _ParentLocationID = value;
            }
        }
    }

    [Column]
    public int SiteID { get; set; }
    [Column]
    public bool Active { get; set; }

region Method

    public static IEnumerable<Location> LoadActiveLocations(int siteID)
    {
        iThNkContext db = new iThNkContext();

        var LocationList = (from l in db.Locations
                                where(l.SiteID == siteID && l.Active == true)
                                orderby l.LocationID
                                select l).ToList();

        return LocationList;
    }

And here is the code I am using in my .aspx file

RadTreeView trvLocation = (RadTreeView)cboLocation.Controls[2].FindControl("trvLocation");
         if (trvLocation.SelectedValue != "")
         {
            var locationID = Convert.ToInt32(trvLocation.SelectedValue);  //Error
            predicates.Add(p => p.LocationID == locationID);
        }

On the //Error line is where I am getting the 'Input string was not in the correct format' error, any suggestions please. I cant understand why I am having this problem

thank you in advance

4
  • The error means that the string you're trying to parse an integer from doesn't actually contain a valid integer. Use Int.TryParse Commented Apr 16, 2013 at 10:31
  • 3
    put a breakpoint and see what you get in trvLocation.SelectedValue Commented Apr 16, 2013 at 10:31
  • 1
    Have you looked at what trvLocation.SelectedValue returns? What type and/or value? Commented Apr 16, 2013 at 10:32
  • What are you binding to the control ? i guess the value part of the RadTreeView is not convertible to int hence the error Commented Apr 16, 2013 at 10:39

2 Answers 2

1

if you expect the locationID to be a number, make sure the the value for each items in the combobox can be converted to numbers. otherwise, even if you use tryparse, your page won't run correctly.

P/S: TryParse will only avoid the error and won't assign the selectedvalue to locationID, and hence, break you logic.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for mentioning that TryParse wont actually fix the problem.
1

It looks like your LocationID is a nullable value - you cannot convert a null value to an Int32. You need to ensure that each value member bound to the drop down list is not a null value but is a valid integer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.