views:

553

answers:

5

hi everyone

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.

+1  A: 

What if you changed

int catID = Int32(CategoryName.SelectedItem);

to

int catID = Int32.Parse(CategoryName.SelectedItem);
David
i tried it.unfortunately doesnt work.
The best overloaded method match for 'int.Parse(string)' has some invalid arguments This is the error it gives.
+1  A: 

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).

Jacob
A: 

Just trying being on the safe side:

int catID = Int32.Parse(CategoryName.SelectedItem.ToString());
Rashack
+2  A: 

I think it should be:

int catID = Int32.Parse(CategoryName.SelectedValue.ToString());
Dmitris
ToString() is not required, SelectedValue is already a string.
Jerome Laban
Oh, thanks! Did not know that.I guess adding ToString() doesn't harm anyways.
Dmitris
A: 

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);
Jerome Laban