Solved

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

Posted on 2003-11-26
12
733 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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 250 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

726 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