Reputation: 75
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
Upvotes: 7
Views: 32429
Reputation: 11
DataRow filteredRows = dt.Select(“Name LIKE '%” + searchstring + “%’
OR
convert(Age, System.String) like '%” + searchstring + “%'”);
It works for me
Upvotes: 0
Reputation: 57956
Probably your GenreID column is an INTEGER
and you're comparing it with a STRING
as in '**'
. In this sample I used the IN
operator to list all values at once without the AND operator between them, what would cause a logic error as none items would have GenreID=1 and 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();
}
Upvotes: 1
Reputation: 504
Filter in your condition might be invalid so Use OR
at the place of AND
Use
%
, insted of*
inLIKE
OperatorI.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();
}
}
Upvotes: 0
Reputation: 11741
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.
Upvotes: 18