Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Introduction:

Here I will explain how to check a dataset contains data or dataset is null or empty in asp.net.

Description:

In previous posts I explained many articles relating code snippets in asp.net. Now I will explain how to check if dataset contains any data or dataset is null or empty in asp.net.

If we want to check dataset contains data or not for that we need to write code as shown below

C# Code


SqlDataAdapter da=new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
string str = string.Empty;
// Condition to check if it contains data or not
if(ds.Tables.Count>0)
{
// Condition to check if dataset tables contains data or not
if (ds.Tables[0].Rows.Count > 0)
{
str = "Dataset table contains data";
}
}
else
{
str = "Dataset does not contains data";
}
VB.NET Code


Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds)
Dim str As String = String.Empty
' Condition to check if it contains data or not
If ds.Tables.Count > 0 Then
' Condition to check if dataset tables contains data or not
If ds.Tables(0).Rows.Count > 0 Then
str = "Dataset table contains data"
End If
Else
str = "Dataset does not contains data"
End If
Here I explained how to check if dataset contains data or null or empty and how to check dataset datatable contains data or not. 

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

2 comments :

Anonymous said...

Hello,

I guess if ds is null then your code ds.tables.count will throws an exception.

Manish Swarnkr said...

Sir ,
How to know count empty field in a Particular row And how to know empty field in a row ..

Give your Valuable Comments

Other Related Posts

© 2010-2012 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.