Solved

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

Posted on 2003-11-26
12
735 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
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.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

628 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