display in label on c#form number of records from microsoft access database

davetough
davetough used Ask the Experts™
on
hello,
need to display number of records from microsoft database in a label on C# form.
I have a C# form connected to microsoft access database.
am not sure of where to start
thank you
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Hello ,

Yuo want to display no of record from MS Access Database Table in Label Control.

Commented:
Please provide information about which version of Access you are suing ?

I attached sample code . you need to set connection string and table name.

OleDbConnection con = new OleDbConnection();
            con.ConnectionString = "your oledb connection string";
            OleDbCommand cmd = new OleDbCommand();
            cmd.CommandText = "Select count(*) from [your table name]";
            cmd.Connection = con;
            con.Open();
            Label1.Text = "No of Record " + cmd.ExecuteScalar().ToString();
            con.Close();

Open in new window

Commented:

OleDbConnection con = new OleDbConnection();
            con.ConnectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}", @"office 2007 access db full path over here");
            OleDbCommand cmd = new OleDbCommand();
            cmd.CommandText = "Select count(*) from customers";
            cmd.Connection = con;
            con.Open();
            label1.Text = "No of Record " + cmd.ExecuteScalar().ToString();
            con.Close();

Open in new window

Commented:
for office 2003.

OleDbConnection con = new OleDbConnection();
            con.ConnectionString = String.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Persist Security Info=False", @"your mdb file path over here.");
            OleDbCommand cmd = new OleDbCommand();
            cmd.CommandText = "Select count(*) from customers";
            cmd.Connection = con;
            con.Open();
            label1.Text = "No of Record " + cmd.ExecuteScalar().ToString();
            con.Close();

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial