Solved

C#: how to find out if returned dataTable is empty?

Posted on 2003-11-26
12
726 Views
Last Modified: 2010-04-16
Hi again...
I make a query to our database and get a result back.
I need to make the check if the something was retunred form the DB or nothing.

-------- in VB it was pretty simple: --------
set SQLresult = connection.execute("SELECT...whatever...")
if (SQLresult.EOF) then
    'error!
else
   'do a loop
end if
--------


What is the usual way to do this in C#?

Thx
0
Comment
Question by:Smoerble
  • 4
  • 3
  • 2
  • +3
12 Comments
 
LVL 10

Expert Comment

by:ptmcomp
Comment Utility
Here you can find sample code for DataReader:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconTheADONETDataReader.asp

If you have a DataTable (needs to be loaded by a Sql/OleDBDataAdapter) you can just check dataTable.Rows.Count
0
 

Expert Comment

by:Shriraj
Comment Utility
You can use try, catch method.

Example:

try
{
    //do something or get data from database
}
catch
{
    MessageBox.Show(" No data Found");
}

If you will get record, you can do what ever you want and if there is no data you will see message.    
0
 
LVL 9

Expert Comment

by:malharone
Comment Utility
           System.Data.DataSet ds=new  System.Data.DataSet("TestDataSet");
            System.Data.SqlClient.SqlConnection conn=new System.Data.SqlClient.SqlConnection("YOUR CONNECTION STRING");
            System.Data.SqlClient.SqlCommand sc= new System.Data.SqlClient.SqlCommand("Select * from faculty",conn);
            System.Data.SqlClient.SqlDataAdapter sda = new  System.Data.SqlClient.SqlDataAdapter(sc);
            sda.Fill(ds);
            if (ds==null || ds.Tables.Count<1 ||ds.Tables[0].Rows.Count<1)
            {
                MessageBox.Show ("Error in data!", "My Application", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                //no error
            }
0
 
LVL 10

Expert Comment

by:ptmcomp
Comment Utility
@Shriraj:
This is dirty programming unless there must be data and else it's an exceptional application state = exception.
0
 
LVL 20

Expert Comment

by:TheAvenger
Comment Utility
OleDbConnection conn = new OleDbConnection ("connString");
OleDbCommand comm = new OleDbCommand();
comm.CommandText = "SELECT * FROM Table";
comm.Connection = conn;
conn.Open();
OleDbDataReader reader = comm.ExecuteReader();
if (!reader.HasRows)
  do whatever needed when there is no data

while (reader.Read()) {
  do whatever needed with the data;
  it will come here only if data is available
}

reader.Close();
conn.Close();
0
 

Author Comment

by:Smoerble
Comment Utility
Why has "TheAvenger" a complete different code than "malharone"?
Which one is betetr to use when?
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:Smoerble
Comment Utility
Increased points to 250...
0
 
LVL 20

Expert Comment

by:TheAvenger
Comment Utility
What else do you need? The solution I gave you works fine...
0
 

Author Comment

by:Smoerble
Comment Utility
Please read my comments:
"Why has "TheAvenger" a complete different code than "malharone"?"
I understand that your code works fine, but there has to be a reason why you don't use the SqlDataAdapter... before I change my current code (currently using it), I would like to know _why_ you do it in a different way (I am sure you have a good reason because your other answers in other threads showed good reasons too).

Also please tell me how you would itterate through the result when using your code.
Currently I do it with:

DataSet dtSet = new DataSet();
myCmd.Fill( dtSet, "Developer" );
DataTable dTable = dtSet.Tables[0];foreach( DataRow dtRow in dTable.Rows )
{
   ...
}
0
 
LVL 20

Accepted Solution

by:
TheAvenger earned 250 total points
Comment Utility
Using OleDbXxx objects instead of SqlXxx objects is made when you use an OLE DB provider. SqlXxx object work ONLY with SQL Server (version 7 and above I think), while OleDbXxx work for all OLE DB and ADO.NET supported sources. If you are using SQL Server and you are sure that you will never change to another data source, stay with SqlXxx (just replace the OleDb part with Sql in my code, it will work again).

Using data set and data table is memory and time consuming. If you only need to read the data once, my code iterates over the results in this part:

while (reader.Read()) {
  do whatever needed with the data;
  it will come here only if data is available
}

If you need to access the data later, you should decide how to keep it (maybe put it in an array list or in this case use a DataTable. DataSet is still not needed but can be used as well).
0
 

Author Comment

by:Smoerble
Comment Utility
thanks for the answer. Points granted.
@malharone:
please contact me to recieve additional points even though I don't use your solution.
0
 
LVL 2

Expert Comment

by:BorlandMan
Comment Utility

If you have not used a datareader, you should probably be aware of the following:

you can only
- move forward only - cannot move backward in the list.
- and the data is read-only - cannot edit anything.
... but it is very good in performance.


just a friendly fyi


sincerely,
J
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now