local variable named 'selectedData' cannot be declared

Sorry, I'm a newbie on C#. I'm doing a basic online course and I've mofified the code from one of the examples as I'm trying to write a program which can display a list of movies of a particular genre which titles contain a given string.

I get the following error message when I try to launch the app on a browser:

Compiler Error Message: CS0136: A local variable named 'selectedData' cannot be declared in this scope because it would give a different meaning to 'selectedData', which is already used in a 'parent or current' scope to denote something else

=================================================================

Source Error:

Line 12: searchGenreS = Request.QueryString["searchGenre"]; Line 13: searchTitleS = "%" + Request["searchTitle"] + "%"; Line 14: var selectedData = db.Query(selectCommand, searchGenreS, searchTitleS); Line 15:
Line 16: }

Source File: c:\Users\Charles\Documents\My Web Sites\WebPagesMovies\MoviesCombinedQuery.cshtml Line: 14

Here is the code:

@{
var db = Database.Open("WebPagesMovies") ;
var selectCommand = "SELECT * FROM Movies";
var searchTerm = "";
var searchGenreS = "";
var searchTitleS = "";
var selectedData = "";

if((!Request.QueryString["searchGenre"].IsEmpty() ) && (!Request.QueryString["searchTitle"].IsEmpty()  ) )
{
    selectCommand = "SELECT * FROM Movies WHERE Genre = @0 AND Title LIKE @1";
    searchGenreS = Request.QueryString["searchGenre"];
    searchTitleS = "%" + Request["searchTitle"] + "%";
    var selectedData = db.Query(selectCommand, searchGenreS, searchTitleS);

}
else
{
  if(!Request.QueryString["searchGenre"].IsEmpty() )
   {
    selectCommand = "SELECT * FROM Movies WHERE Genre = @0";
    searchTerm = Request.QueryString["searchGenre"];
   }

  if(!Request.QueryString["searchTitle"].IsEmpty() ) 
   {
    selectCommand = "SELECT * FROM Movies WHERE Title LIKE @0";
    searchTerm = "%" + Request["searchTitle"] + "%";
   }

  var selectedData = db.Query(selectCommand, searchTerm);

}   
 var grid = new WebGrid(source: selectedData, defaultSort: "Genre", rowsPerPage:3);

}

Can anyone help me??

Thanks!!

Cheers!!

Jon Skeet
people
quotationmark

You're redeclaring selectedData in the if and else bodies. You don't want to do that - you've already declared the variable earlier. You just need to change those statements into assignments to the existing variable. So this:

var selectedData = db.Query(selectCommand, searchGenreS, searchTitleS);

becomes:

selectedData = db.Query(selectCommand, searchGenreS, searchTitleS);

And this:

var selectedData = db.Query(selectCommand, searchTerm);

becomes:

selectedData = db.Query(selectCommand, searchTerm);

I strongly suspect you'll need to change the type of selectedData though - currently it's a string given its initial value - does db.Query return a string as well? That would seem unlikely to me.

EDIT: As noted, it looks like you just need to decalre selectedData like this:

IEnumerable<dynamic> selectedData = null;

people

See more on this question at Stackoverflow