3

In my case i am passing a sql query and getting data in dataset, but problem occurs when i try to get the rows where ParentId column contain NULL.This is the piece of code.

   DataSet ds = GetDataSet("Select ProductId,ProductName,ParentId from ProductTable");

   //ds is not blank and it has 2 rows in which ParentId is NULL

   DataRow[] Rows = ds.Tables[0].Select("ParentId IS NULL");

But still i am not getting any rows. Need help. Thanx.

1
  • The way you're doing seems correct to me and it should be working, but make sure that it has null and not blank value. Commented Mar 20, 2013 at 12:59

6 Answers 6

13

Use the strongly typed DataRow extension method Field which also supports nullable types:

IEnumerable<DataRow> rows = ds.Tables[0].AsEnumerable()
    .Where(r => !r.Field<int?>("ParentId").HasValue);

Note that i've used Enumerable.Where which is a Linq extension method to filter the table.

If you want an array use ToArray, if you want a new DataTable use CopyToDataTable. If you simply want to enumerate the result use foreach.

foreach(DataRow row in rows)
{
    // ...
}
Sign up to request clarification or add additional context in comments.

8 Comments

still it is showing this- base {System.SystemException} = {"Specified cast is not valid."} . when i write string instead of int it shows- {The type 'string' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable<T>} .i am not familier with linq.
@Anand; Then the type of the DataColumn is string instead of int(as i've presumed). You have to use .Where(r => r.Field<string>("ParentId")==null) instead.
now it is showing- {Empty = "Enumeration yielded no results"}...why so ? :(
@Anand: Because there are no rows with ParentId=null?! Can you show the last query you have tried?
DataSet ds = GetDataSet("Select ProductId,ProductName,ParentId from ProductTable"); IEnumerable<DataRow> Rows = ds.Tables[0].AsEnumerable().Where(r => r.Field<string>("ParentId")==null); there is no code between these two lines and believe me data set contain 10 rows and two of them has null in parentid column. even i don't know what the problem is, tired of doing this.
|
5
var rowsWithoutParent = dt.AsEnumerable().Where(r => r["ParentId"] == null);

var rowsWithParent = dt.AsEnumerable().Where(r => r["ParentId"] != null);

2 Comments

it is counting all the rows, how to count only null
This answer guide me to check if a column value is empty when reading from a csv file. For me it was like - data.AsEnumerable().Where(r => r["SHORT_CODE"].ToString() == "").Any();
3
var rows = ds.Tables[0].AsEnumerable()
    .Where(r => r.IsNull("ParentId"));

Comments

0

work for me:

   DataTable newDt = dt.AsEnumerable().Where(r => r["column name"] == DBNull.Value).CopyToDataTable();

if equal to null is not work because is return DBNull

1 Comment

Although this code might answer the question, a good answer should always contain an explanation of the code.
-1

Check

ds.Tables.Count

then

ds.Tables[0].Rows.Count

Comments

-1

You can access the items like this:

 String value = ds.Tables[0].Rows[RowNum][ColNum].ToString();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.