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
Solved

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

Posted on 2003-11-26
12
732 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
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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…
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 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