Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter data from DataTable

Tags:

c#

linq

DataTable dtt = (DataTable)Session["ByBrand"];
var filldt = (dtt.Select("Price >= " + HiddenField1.Value + " and Price <= " + HiddenField2.Value + "")).CopyToDataTable();

this code is working fine when it found values in selected DataTable but it showing error when Values are not found in DataTable. So please tell me how to check if no record found.

like image 357
Gopal Avatar asked Dec 15 '22 10:12

Gopal


1 Answers

Simply check if your Select returns anything?

 DataTable dtt = (DataTable)Session["ByBrand"];
 DataRow[] rows = dtt.Select("Price >= " + HiddenField1.Value + " and Price <= " + HiddenField2.Value + "");
if(rows.Length > 0)
{
    var filldt = rows.CopyToDataTable();
}

Well, the Linq example from Tim is really nice, but to complete my answer. The Select method returns Always a DataRow array also if there is no row selected, but then you cannot ask to build a datatable from this empty array. Think about it. What schema the CopyToDataTable should build for the resulting table if no rows are present in the array?

like image 50
Steve Avatar answered Jan 26 '23 00:01

Steve