Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Cannot perform '=' operation on System.Int32 and System.String" while performing a search

Tags:

c#

.net-3.5

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();
        }
    }

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?

Thanks in advance

like image 874
Barker Avatar asked Dec 05 '22 21:12

Barker


1 Answers

This is not compile time error this is runtime error.

Well in your database the thing for which you are applying filter is type of 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

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

like image 191
Neel Avatar answered Dec 08 '22 11:12

Neel