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
GenreID='2'
or something, perhaps it is treating it as a string but the column is integer, try removing all the extraneous formatting and create a filter that just doesGenreID=2
. Can you provide an example of whatfilter
contains when it is being set? Also as Igor points out, your chaining ofAND
expressions cannot just daisy chain values, you need to specify the field each time too:GenreID=2 AND GenreID = 3
. – Bulbousfilter
contains just before you try to set it to the data view. It is very likely incorrectly formed. Please see: csharp-examples.net/dataview-rowfilter – Bulbous