I have a database in which

customerid CustomerName
1 ------------- Yousaf
2 ------------- Aslam
3 -------------Manga
4 ------------- ABC
=================================================
Now I want Count customerid Row. How can i do it
In SQL SERVER I write the query

Select Count(Customerid) from studentinfo

And I get Total Customer.


I want display total customerid in label
Please Help me As soon As Possible
Thanks in Advance

Recommended Answers

All 10 Replies

public void SetLable1(string connString) {
    int  totalCustomer = 0;
    string sql = "SELECT COUNT(*) FROM studentinfo";
    using (SqlConnection conn = new SqlConnection(connString)) {
        SqlCommand cmd = new SqlCommand(sql, conn);
        try {
            conn.Open();
            totalCustomer = (Int32)cmd.ExecuteScalar();
        } catch (Exception ex) {
            Console.WriteLine(ex.Message);
        }
    }
    label1.Text = totalCustomer.ToString();
}

you need something like this (i might have made a few mistakes as im still new ish to c#, i did something similar in my uni project)

public void setLable()
{
	List<string> customerids = new List<string>();

	customerids = getCustonerID();
	
	foreach (string id in customerids)
	{
	    Lable1.TEXT = "ID = " + id "\n";
	}
}


public List<string> getCustiomerID()

	    string sql = "SELECT Custoberid FROM mydatabase.studentinfo";


            SqlCommand cmd2 = new SqlCommand(sql, db.Connect());

            List<string> CustomeridList = new List<string>();

            try
            {
                // Creates SQLdatareader
                SqlDataReader reader2;

                // Reads the result of cmd command
                reader2 = cmd2.ExecuteReader();

                // Loops while there is data to available to read
                while (reader2.Read())
                {
                    // Adds each ID read to a list of strings
                    CustomeridList.Add(reader2["Customerid "].ToString());
                }
                //Closes reader
                reader2.Close();
            }
            //Executed if try cannot execute
            catch (Exception ex)
            {
		throw Exception("" + ex);
            }

            //Returns string list
            return objectIDlist;

I am not Giving the result Please more help
My Connection String is as below

string conn = "Data Source = YS-8462BA359936;Initial Catalog=Schooldata;Integrated Security = True";
SqlConnection objCon = new SqlConnection(conn);
SqlDataAdapter objDA = new SqlDataAdapter("SELECT COUNT(Studentid) from studentinfo", objCon);

how can I get total result in label1

How come?
if connection string is ok, and table is not empty, Momerath`s solution has to work.
His query and use of ExecuteScalar method returns the number of rows.

sorry i forgot to put

string sql = "SELECT COUNT(Custoberid) FROM mydatabase.studentinfo";

When I want get result in label it gives me error

string conn = "Data Source = YS-8462BA359936;Initial Catalog=Schooldata;Integrated Security = True";
SqlConnection objCon = new SqlConnection(conn);
SqlDataAdapter objDA = new SqlDataAdapter("SELECT COUNT(Studentid) from studentinfo", objCon);
label1.text = objda;

The total of studentid not seen in label.When I type code with datagrid result always correct

DataTable objDT = new DataTable();
this.dataGridView2.DataSource = objDT;

but I want get total result in label

have you tried my solution ? i might be a bit longer but will still do the job. However do this in the line that says label.TEXT = . . .

lable1.Text = lable1.Text + "ID = " + id "\r\n";

What I Do ?My Problem is still.
Now I Run Momerath Code But It get result -1.But in my studentid table there are 10 Rows.this is code

private void button5_Click(object sender, EventArgs e)
        {
       string conn = "Data Source = YS-8462BA359936;Initial Catalog=Schooldata;Integrated Security = True";

            int Studentid = 0;
            SqlConnection objCon = new SqlConnection(conn);
            objCon.Open();            
            SqlCommand cmd = new SqlCommand("select studentid from studentinfo", objCon);
            Studentid = cmd.ExecuteNonQuery();
            
            objCon.Close();
            
            MessageBox.Show("Total Number Of StudentID are! "+Studentid.ToString());
        }

Please Friends Help me I am In big trouble

That's not the code I posted. You are using ExecuteNonQuery when you are trying to execute a query. Your SQL statement is wrong. Go back and reread what I posted and try it exactly as it is written, not whatever changes you decided to make.

Oh Sorry Momerath This was my fault I was not able to use your code.Now This Problem is completely Solved

public void SetLable1(string connString)
        {
            connString = "Data Source = YS-8462BA359936;Initial Catalog=Schooldata;Integrated Security = True";

            int totalCustomer = 0;
            string sql = "SELECT COUNT(Studentid) FROM studentinfo";
            using (SqlConnection conn = new SqlConnection(connString))
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                try
                {
                    conn.Open();
                    totalCustomer = (Int32)cmd.ExecuteScalar();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            label1.Text = totalCustomer.ToString();
        }
private void button1_Click(object sender, EventArgs e)
        {
            SetLable1("connString");
        }

Thanks forMomerath,ChrisHunter,
Mitja Bonca

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.