Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2003-11-26
12
Medium Priority
?
736 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +3
12 Comments
 
LVL 10

Expert Comment

by:ptmcomp
ID: 9826281
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
ID: 9826614
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
ID: 9826628
           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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 10

Expert Comment

by:ptmcomp
ID: 9826666
@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
ID: 9830233
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
ID: 9836633
Why has "TheAvenger" a complete different code than "malharone"?
Which one is betetr to use when?
0
 

Author Comment

by:Smoerble
ID: 9836640
Increased points to 250...
0
 
LVL 20

Expert Comment

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

Author Comment

by:Smoerble
ID: 9836700
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 1000 total points
ID: 9836767
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
ID: 9836811
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
ID: 9838430

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

715 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