对不起,我是 C# 的新手。我正在做一个基本的在线课程,并且我已经修改了其中一个示例中的代码,因为我正在尝试编写一个程序,该程序可以显示特定类型的电影列表,其中标题包含给定的字符串。
当我尝试在浏览器上启动应用程序时收到以下错误消息:
编译器错误消息:CS0136:无法在此范围内声明名为“selectedData”的局部变量,因为它会给“selectedData”赋予不同的含义,“selectedData”已在“父级或当前”范围内用于表示其他内容
==================================================== ================
源错误:
第 12 行:searchGenreS = Request.QueryString["searchGenre"]; 第 13 行:searchTitleS = "%" + Request["searchTitle"] + "%"; 第 14 行: var selectedData = db.Query(selectCommand, searchGenreS, searchTitleS); 第 15 行:
第 16 行:}
源文件:c:\Users\Charles\Documents\My Web Sites\WebPagesMovies\MoviesCombinedQuery.cshtml 行:14
这是代码:
@{
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);
}
谁能帮我??
谢谢!!
干杯!!