Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello,

I want to know wether is it possible to check that a data exist or not in a mysql database table using c# winform application. This application will pop-up a message "Your Table is Empty!" when I will run this module. If it contains data then It will show a message "Data exist".

What I have tried:

I have tried this code :

MySqlCommand cmd = new MySqlCommand(@"SELECT COUNT(*) from tablename", con);
Posted
Updated 8-Dec-22 2:20am
Comments
Member 15627495 8-Dec-22 7:54am    
you have to execute the query, then "if row.count == 0 then popup("empty table").
0x01AA 8-Dec-22 8:10am    
And what happened when you tried your code?

1 solution

That single line of code does nothing useful on its own. You have to actually execute that command object you setup, using cmd.ExecuteScalar:
C#
int rowCount = (int)cmd.ExecuteScalar();

Then you can check the value of rowCount and make your decision.
 
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