fdon
fdon

Reputation:

Asp.net, Linq Error: 'int' is a 'type' but is used like a 'variable'

i am using asp.net and link to display a set of books from the db. n it gives me an error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0118: 'int' is a 'type' but is used like a 'variable'

Source Error:

Line 48:         using(MobileBooksDataContext categoryList = new MobileBooksDataContext())
Line 49:         {
Line 50:             int catID = Int32(CategoryName.SelectedItem); 
Line 51:             var newBookList = from b in categoryList.team5_bookmobiles
Line 52:                               where(b.ca_id == catID)


protected void getBookList()
    {

        using(MobileBooksDataContext categoryList = new MobileBooksDataContext())
        {
            int catID = Int32(CategoryName.SelectedItem); 
            var newBookList = from b in categoryList.team5_bookmobiles
                              where(b.ca_id == catID)
                            select new
                            {
                                lblBook_name = b.book_name,
                                lblBook_author = b.book_author,
                                lblBook_shortdesc = b.book_short_desc
                            };

            lv_Books.DataSource = newBookList;
            lv_Books.DataBind();
        }
    }

    protected void btn_Select_Click(object sender, EventArgs e)
    {
        getBookList();
    }

i take the category id from a drop down list and match it against the category id of books that is in a different table.

Upvotes: 0

Views: 1641

Answers (5)

Dmitri S.
Dmitri S.

Reputation: 3438

I think it should be:

int catID = Int32.Parse(CategoryName.SelectedValue.ToString()); 

Upvotes: 2

Jérôme Laban
Jérôme Laban

Reputation: 5282

If you are using DataBinding, SelectedItem is most probably the object being databound. You can use it, but you'll have to cast it first. This might be better performance wise, since you will not have to perform a string parse to get your category Id.

Or, you can use the property DropDownList.SelectedValue, which you may have configured using the databinding wizard, or the property DataValueField on the control's declaration.

Either way, you cannot use a C++ or VB like syntax to perform a cast to an Int32 using C#.

If you use the SelectedValue property, you'll have a string and to convert it to an Int32, you'll have to write something like this :

var value = Int32.Parse(CategoryName.SelectedValue, CultureInfo.InvariantCulture);

Upvotes: 0

Rashack
Rashack

Reputation: 4665

Just trying being on the safe side:

int catID = Int32.Parse(CategoryName.SelectedItem.ToString());

Upvotes: 0

Jacob Krall
Jacob Krall

Reputation: 28835

Line 50 is using the Int32 constructor like a function. Change Int32(CategoryName.SelectedItem) to new Int32(CategoryName.SelectedItem), or just cast to int using (Int32)(CategoryName.SelectedItem).

Upvotes: 1

David
David

Reputation: 34563

What if you changed

int catID = Int32(CategoryName.SelectedItem); 

to

int catID = Int32.Parse(CategoryName.SelectedItem); 

Upvotes: 1

Related Questions