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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With