Subscribe to Subscribers

Forums » .NET » .NET »

Datatable null value check


Posted Date: 22 May 2012      Posted By:: bhavani b     Member Level: Silver    Member Rank: 1610     Points: 3   Responses: 6



Hi,

If table having columns(header) but no rows...I need to to check the table is empty or not.
I have checked like this but
if(dtMenu != null && dtmenu.rows.count>0)
{
//TO-DO Here
}
Even though if rows are not there it is going into loop.How can i handle that situation
Please can you help me out

Thanks
Sandhya




Responses

#671926    Author: Prachi Kulkarni        Member Level: Gold      Member Rank: 33     Date: 22/May/2012   Rating: 2 out of 52 out of 5     Points: 3

Hi,
DBNull.Value and column value can be checked for finding null values.

foreach(DataRow row in dtMenu.Rows)
{
object value = row["ColumnName"];
if (value == DBNull.Value)
{
Console.Writeln("Null");
}
else
{
Console.Writeln("Not Null");
}
} //end of foreach


Regards,
Prachi Kulkarni.


 
#671928    Author: Anil Kumar Pandey      Member Level: Platinum      Member Rank: 1     Date: 22/May/2012   Rating: 2 out of 52 out of 5     Points: 2

The Row count will not return you any values if there is no row.
So this will work, if Dt is a data table,


if(dtmenu.Rows.count>0)
{
//Ur code here
}


if it is a Data set then


if(dtmenu.Tables[0].Rows.count>0)
{
//Ur code here
}


Thanks & Regards
Anil Kumar Pandey
Microsoft MVP, DNS MVM


 
#671939    Author: Ajatshatru Upadhyay      Member Level: Gold      Member Rank: 17     Date: 22/May/2012   Rating: 2 out of 52 out of 5     Points: 3

Hi,

For Datareader:

SqlDataReader sdr= new SqlDataReader();
While(sdr.Read()
{
/your code…
}
While(


For DataTable:

while(DataTabe.Rows.Count)
{
// your code…
}


For DataSet:

DataSet ds = new DataSet();
While(ds.Tables[0].Rows.Count)
{
// your code…
}


Hope it'll help you.
Regards
Ajatshatru





 
#671945    Author: Asheej T K   Online     Member Level: Diamond      Member Rank: 2     Date: 22/May/2012   Rating: 2 out of 52 out of 5     Points: 2

Hi,

Remove dtMenu != null check form the code. It won't retur null so your condition won't satify.

Change your code like below,

if(dtmenu.rows.count>0)
{
//TO-DO Here
}

Let me know if this doesn't works.


Regards,
Asheej T K
Microsoft MVP[ASP.NET/IIS]
DotNetSpider MVM

Dotnet Galaxy


 
#671960    Author: sudhajosyula      Member Level: Gold      Member Rank: 46     Date: 22/May/2012   Rating: 2 out of 52 out of 5     Points: 2

Remodify your code with the below code and see if it works


if (dtmenu!= null)
{
if(dtmenu.Rows.Count>0)
{
//DoSomething
}
}


 
#672073    Author: sandy      Member Level: Gold      Member Rank: 330     Date: 22/May/2012   Rating: 2 out of 52 out of 5     Points: 2

if (dtCash != null)
{
if (dtCash.Rows.Count > 0)
{
}

}

Regards
sandy


 
Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.



Next : Difficulty in using text changed property of text box
Previous : Scheduler utility /windows service
Return to Discussion Forum
Post New Message
Category:

Related Messages

Awards & Gifts
Talk to Webmaster Tony John
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
2005 - 2013 All Rights Reserved.
.NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
Articles, tutorials and all other content offered here is for educational purpose only.
We are not associated with Microsoft or its partners.