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

how to check dataset is null or empty in asp.net | Check dataset contains a data or not

Jul 17, 2012
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

5 comments :

Anonymous said...

Hello,

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

Manish Swarnkar said...

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

Vicky said...

If ds is null try this..

if (ds != null && ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
}
else
{
ScriptManager.RegisterStartupScript(this, GetType(), "", "alert('Invalid ID (or) No Data to Display..!');", true);
}

Unknown said...

hello sirr i want to chq my stock quantity if sale all items then show msg stock is empty

Markand s Bhatt said...

jjj

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 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.