Reputation: 99
DataTable.Select("agent=98788") method is not returning any rows while agent 98788 is exsting in DataTable. Other agents are getting filtered, only having problem with this agent 98788. Any Idea why this number is not getting filtered using DataTable.Select() method?
DataTable dt = new DataTable();
dt.Columns.Add("agent", typeof(string));
dt.Columns.Add("shiftdate", typeof(string));
dt.Columns.Add("agentoid", typeof(string));
string sourceAgent = "98788";
string targetAgent = "881757";
dt.Rows.Add("98788","2017-10-27","");
dt.Rows.Add("881757", "2017-10-27", "");
DataRow[] drr1 = dt.Select("agent=" + sourceAgent + "");
DataRow[] drr2 = dt.Select("agent=" + targetAgent + "");
drr1 is empty while drr2 is not empty. My confusion is why drr2 is not empty and drr1 is empty. Why "881757" is filterable but "98788" is not? C# .Select() method's behavior should be same for all cases, But in my case it seems it is behaving differently. Is it a bug in DataTable.Select()?
Upvotes: 0
Views: 7935
Reputation: 24137
You need to use single quotes to specify a filter on string values:
DataRow[] drr1 = dt.Select("agent='" + sourceAgent + "'");
DataRow[] drr2 = dt.Select("agent='" + targetAgent + "'");
More about DataView RowFilter Syntax: http://www.csharp-examples.net/dataview-rowfilter/
Quote:
Literals
String values are enclosed within single quotes ' '. If the string contains single quote ', the quote must be doubled.
Upvotes: 1