?
Solved

Why do I get a use of Unassigned local variable error when trying to bind List<t> to grid view

Posted on 2012-09-10
1
Medium Priority
?
1,041 Views
Last Modified: 2012-09-10
I'm using Linq to objects in my ASP.NET web form to filter some data that is returned from my database depending on a radio button selection.  I have declared 2 variables, one IEnumerable<t> which is used to store the filtered data and then I declare a List<t> which will be used to bind to the grid view.  When I try to bind the List<t> variable to my gridview I get a use of unassigned local variable error and I'm at a loss as to the problem might be.  Here is my code.

        string criteria = SearchText;
        IEnumerable<Supplier> list;
        List<Supplier> supplierListing;
        SupplierCollection suppliers = new SupplierCollection();
        suppliers = SupplierCollection.GetAll();

        if (searchBy.SelectedItem.Value == "Text")
        {
            list = from supplier in suppliers where supplier.Name.ToUpper().StartsWith(criteria.ToUpper()) select supplier;
            supplierListing = list.ToList();
        }
        else if (searchBy.SelectedItem.Value == "Category")
        {
            list = from supplier in suppliers where supplier.CategoryID == categoryListBox.SelectedIndex select supplier;
            supplierListing = list.ToList();
        }

        searchListGrid.DataSource = supplierListing;
        searchListGrid.DataBind();

Open in new window

0
Comment
Question by:obb-taurus
1 Comment
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 2000 total points
ID: 38385217
The problem is because you use the
       if (...)
        {
            ...
            supplierListing = list.ToList();
        }
        else if (...)
        {
            ...
            supplierListing = list.ToList();
        }


You have no "else". In other words, if both conditions are not satisfied - you have unassigned variable "supplierListing".

So, you need to decide what happens if you do not search by text or category (throw ex, for example?).

       if (...)
        {
            ...
            supplierListing = list.ToList();
        }
        else if (...)
        {
            ...
            supplierListing = list.ToList();
        }
        else
        {
            throw new Exception("Wrong criterion");
        }
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month17 days, 2 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question