尝试搜索DataList,参数为电影类型(通过数据库加载,因此没有切换案例)和名称
protected void ButtonFilter_Click(object sender, EventArgs e)
{
string filter = "";
int selectedCount = 0;
for (int i = 0; i < this.CheckBoxListGenres.Items.Count; i++)
if (this.CheckBoxListGenres.Items[i].Selected)
selectedCount++;
if (selectedCount > 0)
{
filter = "GenreID=";
int n = 0; //Used to determine to which genre the loop has arrived
for (int i = 0; i < this.CheckBoxListGenres.Items.Count; i++)
{
if (this.CheckBoxListGenres.Items[i].Selected)
{
if (n > 0 && n < selectedCount)
filter += " AND ";
filter+="'*"+this.CheckBoxListGenres.Items[i].Value.ToString()+"*'";
n++;
}
}
if (this.TextBoxMovieName.Text!="")
filter += " AND MovieName LIKE '*" + this.TextBoxMovieName.Text + "*'";
DataTable dataTable = ((DataSet)Application["DataSetMovies"]).Tables[0];
DataView dataView = new DataView(dataTable);
filter = Convert.ToString(filter);
dataView.RowFilter = filter; //!Getting the error here!
this.DataListMovies.DataSource = dataView;
this.DataListMovies.DataBind();
}
}
尝试调试,字符串本身似乎很好,所以我尝试在过滤器上使用Convert.ToString(),只是为了确保但无关紧要。 帮助
提前致谢
答案 0 :(得分:10)
这不是编译时错误,这是运行时错误。
在您的数据库中,您要应用过滤器的东西是Int32 ..
的类型例如,如果你有类似Num
的内容,而且它是Int32
,但你会做如下事情: -
dv.RowFilter = "Num = '7097'" ////This will have error
它会抛出错误,因为它需要: -
dv.RowFilter = "Num = 7097" ////This is correct
所以在你的情况下,你有GenreID
是Int32,但在你的代码中你提供了一些字符串。
答案 1 :(得分:1)
您的GenreID列可能是INTEGER
,您可以将其与STRING
中的'**'
进行比较。在这个示例中,我使用IN
运算符一次列出所有值,而它们之间没有AND运算符,什么会导致逻辑错误,因为没有项目会有GenreID = 1和GenreID = 2:
protected void ButtonFilter_Click(object sender, EventArgs e)
{
string filter = "1=1";
if (this.CheckBoxListGenres.Items.OfType<ListItem>().Any(i => i.Selected))
filter += String.Format(" AND GenreID IN ({0})'",
String.Join(",", this.CheckBoxListGenres.Items.OfType<ListItem>()
.Where(i => i.Selected).Select(i => i.Value)));
if (this.TextBoxMovieName.Text != "")
filter += " AND MovieName LIKE '%" + this.TextBoxMovieName.Text + "%'";
DataTable dataTable = ((DataSet)MediaTypeNames.Application["DataSetMovies"]).Tables[0];
DataView dataView = new DataView(dataTable);
dataView.RowFilter = filter;
DataListMovies.DataSource = dataView;
DataListMovies.DataBind();
}
答案 2 :(得分:0)
根据您的情况过滤可能无效,因此请在OR
AND
在
中使用%
运营商*
的{{1}}I.E
LIKE
filter += " AND MovieName LIKE '%" + this.TextBoxMovieName.Text + "%'";