Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apostrophe in DataView RowFilter

Tags:

c#

asp.net

I have a DataView that I'm trying to filter based on a dynamic string:

dv.RowFilter = "ContentTitle = '" + titleFilter + "'";

In some cases, titleFilter contains an apostrophe, which is closing out the filter query and causing an error.

Is there a way I can escape that character? I can't replace it.

like image 772
Steven Avatar asked May 03 '13 15:05

Steven


People also ask

What is RowFilter DataView?

Gets or sets the expression used to filter which rows are viewed in the DataView. public: virtual property System::String ^ RowFilter { System::String ^ get(); void set(System::String ^ value); };

Can you use a DataView to filter rows in a DataTable?

Using a DataView, you can expose the data in a table with different sort orders, and you can filter the data by row state or based on a filter expression. A DataView provides a dynamic view of data in the underlying DataTable: the content, ordering, and membership reflect changes as they occur.


1 Answers

Simply double the apostrophe (a.k.a. quote) inside the titleFilter string with

dv.RowFilter = "ContentTitle = '" + titleFilter.Replace("'", "''") + "'";
like image 108
Steve Avatar answered Oct 12 '22 06:10

Steve