0

I have this in my webpage

@{
    if(!IsPost){
        var db = Database.Open("MyDatabase");
        var catList= db.Query("select * from category");    
    }
    else{
        var db = Database.Open("MyDatabase");
        var query = "insert into product (productkey,productname,categorykey) values(@0,@1,@2)";
        ....more code....
    }
}

In my html I have the following

<select id="categorySelect" name="Name">
@foreach(var cat in catList){
    <option value=cat.CategoryKey>@cat.CategoryName</option> 
}
</select>

Problem is that when navigating to this page the first time I get this CS0103 exception saying that The name catList does not exist in the context

I am loading this page from a link in another page. What is wrong in my code?

J. Steen
  • 15,470
  • 15
  • 56
  • 63
E-Bat
  • 4,792
  • 1
  • 33
  • 58

1 Answers1

0

You define catList in if(){...} statement so you get this error, predefine it before if.

dynamic catList=null;
if(!IsPost){
var db = Database.Open("MyDatabase");
catList= db.Query("select * from category");
...  
FLCL
  • 2,445
  • 2
  • 24
  • 45
  • Hey thanks Aleksey, just one clarification to those facing the same problem. catList variable should be declared with type dynamic instead of IEnumerable to avoid generic type exception: dynamic catList=null – E-Bat Sep 17 '12 at 03:40