0

With ADO.net, if I want to retrieve the ID from combobox I just do such like this :

int idToGet = int.parse(tbCategory.SelectedValue.ToString());

Then done,

But when I bind the combobox with EF, it will show error Input string was not in a correct format. So the current value show { id = 7, category = TESTING } and not as usual.

Here a my code snippet :

    public void loadCategory()
    {
        tbCategory.DataSource = null;

        var listCategoryObj = new malsic_inventoryEntities();

        var query = from cat in listCategoryObj.Category
                    //join cat in listItmObj.Category on n.id_category equals cat.id
                    select new { cat.id,cat.category };

        if (query.Count() > 0)
        {
            tbCategory.DataSource = query.ToList();
            tbCategory.DisplayMember = "category";
            tbCategory.ValueMember = "id";
            tbCategory.Invalidate();
        }
        else
        {
            tbSubCategory.Enabled = false;
        }

    }
    public void loadSubcategory()
    {
        tbSubCategory.DataSource = null;

        int id_category_current = int.Parse(tbCategory.SelectedItem.Value.ToString());

        var listSubCategoryObj = new malsic_inventoryEntities();

        var query = from SubCat in listSubCategoryObj.SubCategories
                    where SubCat.id_category == id_category_current
                    select new { SubCat.id, SubCat.subcategory };

        if (query.Count() > 0)
        {
            groupBox1.Enabled = true;

            tbSubCategory.DataSource = query.ToList();
            tbSubCategory.DisplayMember = "subcategory";
            tbSubCategory.ValueMember = "id";
            tbSubCategory.Invalidate();
        }
        else
        {
            groupBox1.Enabled = false;
        }

    }

I do something wrong?

Azri Zakaria
  • 1,324
  • 5
  • 23
  • 52
  • Look at your stack trace. What line is your error on? Are you trying to parse a string that isn't an int? http://stackoverflow.com/a/8321527/150342 – Colin Oct 24 '13 at 02:26
  • @Colin, Normally when I use ADO.net then use tbCategory.ValueMember = "id" then it will retrieve the ID number only. But with EF, the ValueMember is not "id", but combination with id and category. How can I get the ID only? – Azri Zakaria Oct 24 '13 at 02:46
  • Use your debugger. What line is your error on? – Colin Oct 24 '13 at 04:42
  • _ValueMember is not "id", but combination..._ What do you mean? You still set `ValueMember = "id"`. The only thing that can go wrong here is that `cat.id` contains strings that do not consist of numbers only, or contains `null` values. – Gert Arnold Oct 24 '13 at 11:36

1 Answers1

0

I don't think your problem is anything to with ADO.NET or Entity Framework. I think your problem is on the line with int.Parse. Try setting id_category_current this way instead of how you do it now:

int id_category_current;
if(!int.TryParse(tbCategory.SelectedItem.Value.ToString(), out id_category_current))
{
   groupBox1.Enabled = false;
   return;
}
Colin
  • 22,328
  • 17
  • 103
  • 197