“无法在System.Int32和System.String上执行'='操作”在执行搜索时 [英] "Cannot perform '=' operation on System.Int32 and System.String" while performing a search

查看:396
本文介绍了“无法在System.Int32和System.String上执行'='操作”在执行搜索时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试搜索DataList,其参数为电影体裁(通过数据库加载,因此没有切换大小写)和名称

Trying to search through a DataList, with the parameters being movie genres (loaded through a database, so no switch case) and a name

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(),只是为了确保但这没关系。
帮助吗?

Tried debugging, the string itself seems fine, so I tried using Convert.ToString() on filter, just to make sure but it doesn't matter. Help?

预先感谢

推荐答案

此不是编译时错误,这是运行时错误。

This is not compile time error this is runtime error.

在数据库中,您要为其应用过滤器的东西是Int32类型。

Well in your database the thing for which you are applying filter is type of Int32..

例如,如果您有类似 Num 的东西,并且它的 Int32 ,但您确实有如下所示:-

For example if you have something like Num and it is of Int32 but you do somthing like below :-

dv.RowFilter = "Num = '7097'" ////This will have error

它会引发错误,因为它需要:-

It will throw error because it will require :-

dv.RowFilter = "Num = 7097" ////This is correct

因此,在您的情况下,您有 GenreID ,它是Int32的,但是在您的代码中,您提供了反对它的字符串。

So in your case you have GenreID which is of Int32 but in your code you are providing some string against it.

这篇关于“无法在System.Int32和System.String上执行'='操作”在执行搜索时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆