无法执行'='系统操作.Int32和System.String"在执行搜索时
本文关键字:执行 quot String 搜索 System 操作 系统 Int32 | 更新日期: 2023-09-27 17:53:08
尝试搜索数据列表,参数为电影类型(通过数据库加载,因此没有切换情况)和名称
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(),只是为了确保,但这并不重要。帮助吗?
Thanks in advance
这不是编译时错误,而是运行时错误。
在你的数据库中你要应用过滤器的对象是Int32..
例如,如果你有一个类似Num
的东西,它是Int32
的,但你做如下操作:-
dv.RowFilter = "Num = '7097'" ////This will have error
会抛出错误,因为它需要:-
dv.RowFilter = "Num = 7097" ////This is correct
在你的例子中,你有GenreID
,它是Int32的但是在你的代码中,你为它提供了一些字符串
可能您的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();
}
过滤器在你的条件可能是无效的,所以使用OR
在AND
的地方
在
中使用LIKE
Operator%
代替*
。E
filter += " AND MovieName LIKE '%" + this.TextBoxMovieName.Text + "%'";
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 += " OR ";
filter+="'%"+this.CheckBoxListGenres.Items[i].Value.ToString()+"%'";
n++;
}
}
if (this.TextBoxMovieName.Text!="")
filter += " OR MovieName LIKE '%" + this.TextBoxMovieName.Text + "%'";
DataTable dataTable = ((DataSet)Application["DataSetMovies"]).Tables[0];
DataView dataView = new DataView(dataTable);
filter = Convert.ToString(filter);
dataView.RowFilter = filter;
this.DataListMovies.DataSource = dataView;
this.DataListMovies.DataBind();
}
}
DataRow filteredRows = dt.Select(“Name LIKE '%” + searchstring + “%’
或
convert(Age, System.String) like '%” + searchstring + “%'”);