Click here to Skip to main content
16,019,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can we count the number of rows in a table?
C#
string dbcnstr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=path.mdf;Integrated Security=True;User Instance=True";
            
SqlConnection conn = new SqlConnection(dbcnstr);
conn.Open();
       
DataSet ds = new DataSet();
int c = ds.Tables["pic"].Rows.Count;
MessageBox.Show("pic table contains "+ c+" no of rows");
conn.Close();

This doesn't work properly. Please can any one help me?
Posted
Updated 31-May-11 8:37am
v3

It looks like you're referring to a table that doesn't exist in the DataSet yet ("pic"). The code you provided creates an empty DataSet, and then tries to access a specific table that simply isn't there.

You'll have to populate the DataSet somehow, perhaps by using a DataAdapter, before trying to access its contents.

I'm guessing that this is what you mean by "this doesn't work properly." If not, please reply to this answer, and provide any error messages or exceptions you're getting, or add them to your original question. Thanks.
 
Share this answer
 
Comments
Member 7779792 31-May-11 14:42pm    
it runs a NullReference Exception. the message says "object reference is not set to an object".
what should i do to correct it?please help me!!! any suggestions?
R. Hoffmann 31-May-11 14:56pm    
This will be because your DataSet is empty - the part that says "ds.Tables["pic"]" returns a null because the "pic" table doesn't exist, and when you then call ".Rows" on it, it throws an exception.

You must understand that a DataSet is simply a container for data, it isn't capable of getting the data by itself. You need to add code to read data from the database connection you instantiated and put it into the DataSet, between where you create the Dataset and where you try to get the rowcount, for this to work. Your existing code is good, it is just a little incomplete.
R. Hoffmann 31-May-11 14:58pm    
And when I say "the pic table doesn't exist", I mean it doesn't exist in the DataSet. It may exist in your actual database, but as I said, the DataSet doesn't know that, it's simply a "dumb" container for data.
Member 7779792 31-May-11 15:40pm    
i include the DataAdapter.now it executes as i expected.thank you very much for your help!i appreciate your help a lot! thank you so much.
R. Hoffmann 31-May-11 15:44pm    
I'm happy to have been of service :)
Well, you could try putting some data in the table...



"table is already containing data."

DataSet ds = new DataSet();
int c = ds.Tables["pic"].Rows.Count;
Since when?
 
Share this answer
 
v2
Comments
Member 7779792 31-May-11 14:29pm    
table is already containing data.
OriginalGriff 31-May-11 14:44pm    
Answer updated
You know, instead of using a DataSet to read an entire table just to get the number of rows, one could ask the database for just the rowcount directly:

C#
string dbcnstr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=path.mdf;Integrated Security=True;User Instance=True";
            
SqlConnection conn = new SqlConnection(dbcnstr);
conn.Open();
       
SqlCommand getRowCount = new SqlCommand("SELECT COUNT(*) FROM pic", conn);
int c = (int)getRowCount.ExecuteScalar();

MessageBox.Show("pic table contains "+ c+" no of rows");
conn.Close();


Of course, this code assumes that you only want to get the number of rows, and not do anything else with the data in your DataSet. If that's not the case then just ignore this answer :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900