Click here to Skip to main content
16,012,166 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
See more:
If i write the following code:

SqlCommand myCommand = new SqlCommand("select * FROM [student_reg] WHERE [std_id]= 'c101234 ' ", myConnection);

it select all value that hold id=c101234 and but how can I count that how many rows there are that contains the id c101234 in C#...

help to ahead.


thanks in advance.
Posted

Your query should be :
SQL
select count(*) from [student_reg] WHERE [std_id]= 'c101234'


Edit :

Also read this : http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar%28v=vs.110%29.aspx[^]

Edit 2:
C#
SqlCommand myCommand = new SqlCommand("select count(*) from [student_reg] WHERE [std_id]= 'c101234' ", myConnection);
int count = (Int32) myCommand.ExecuteScalar();
 
Share this answer
 
v3
Comments
nest101234 29-Mar-14 4:42am    
I want the counting no to save in a variable. then what will be the code, would you pls help me as my query writen code is in below:

SqlCommand myCommand = new SqlCommand("select count(*) from [student_reg] WHERE [std_id]= 'c101234' ", myConnection); myCommand.ExecuteNonQuery();

@mehdi Gholam
Mehdi Gholam 29-Mar-14 4:43am    
See the updated solution.
nest101234 29-Mar-14 4:59am    
there is no clear definition, As I am, beginner , pls make me clear by code with detaiols.... thank you
nest101234 29-Mar-14 5:29am    
Thanks a lot...................
 
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