Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
See more:
i want to total no. of visited patient.
i make following code in c# as:
OleDbCommand vst = new OleDbCommand("select count(*) from old_patient where Contact_no=@Contact_no GROUP BY Contact_no", cn);
                        vst.Parameters.Add(new OleDbParameter("@Contact_no", txtsno.Text));

                        //  store value in rec variable.
                        int rec = (int)vst.ExecuteNonQuery();
                        lbldtr.Text = rec.ToString();


txtsno is textbox;
lbldtr is label where i want to show or print answer.

in txtsno textbox have contact no. so , i want to get particular contact no. visit.

thank u in advance

What I have tried:

OleDbCommand vst = new OleDbCommand("select count(*) from old_patient where Contact_no=@Contact_no GROUP BY Contact_no", cn);
                        vst.Parameters.Add(new OleDbParameter("@Contact_no", txtsno.Text));

                        //  store value in rec variable.
                        int rec = (int)vst.ExecuteNonQuery();
                        lbldtr.Text = rec.ToString();
Posted
Updated 18-Sep-16 23:36pm
v2

You probably have forgotten to write your problem in the question.
Looking at your code, it has following mistakes
1. Query seems to be incorrect
SQL
select count(*) from old_patient where Contact_no=@Contact_no GROUP BY Contact_no

It should be-
SQL
select count(*) from old_patient where Contact_no=@Contact_no

2. You should use ExecuteScalar instead of ExecuteNonQuery
C#
int rec = (int)vst.ExecuteNonQuery();

it should be-
C#
int rec = (int)vst.ExecuteScalar();

Here is a detailed article for detailed reading-
Difference between ExecuteReader ExecuteScalar and ExecuteNonQuery[^]

Hope, it helps :)
If you find further difficulties, please let me know.
 
Share this answer
 
Comments
Karthik_Mahalingam 19-Sep-16 5:39am    
5! much better and clean.
Suvendu Shekhar Giri 19-Sep-16 5:42am    
Thanks @Karthik :)
Member 11572517 19-Sep-16 5:50am    
both of u expert supeerb, it's clear. thank u so muchhhhhhhhhhhhhhhhhhhhhh.
Suvendu Shekhar Giri 19-Sep-16 5:57am    
Thanks for those kind words :)
Glad that it helped resolve your issue.
Maciej Los 19-Sep-16 6:58am    
5ed!
use ExecuteScalar [^]
ExecuteNonQuery will return the number of rows affected, not the actual value which you want to return.

C#
int rec = (int)vst.ExecuteScalar();
 
Share this answer
 
Comments
Suvendu Shekhar Giri 19-Sep-16 5:42am    
Simple and straight :)
5Ed!
Karthik_Mahalingam 19-Sep-16 5:44am    
Thank you SSG :)
Member 11572517 19-Sep-16 5:50am    
thank both of u sir.........
Karthik_Mahalingam 19-Sep-16 5:51am    
welcome :)
Maciej Los 19-Sep-16 6:58am    
5ed!

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