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. |
|||
|
|||
5 comments :
Hello,
I guess if ds is null then your code ds.tables.count will throws an exception.
Sir ,
How to know count empty field in a Particular row And how to know empty field in a row ..
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);
}
hello sirr i want to chq my stock quantity if sale all items then show msg stock is empty
jjj
Note: Only a member of this blog may post a comment.