Display number of Records in Label using C# in ASP.Net

Last Reply one year ago By AnkitPal

Posted one year ago

here is my code,

This code is not in correct form, this code display record, but only 1,

why this code is not displaying my all existing recods

//connectionstring
SqlCommand cmd = new SqlCommand("SELECT COUNT(*) column_name FROM table_name", cnn);
{
    cnn.Open();
    SqlDataReader rdr = cmd.ExecuteReader();
    int count = 0;
    while (rdr.Read())
    {
        count++;
    }
    label1.Text = "(" + count.ToString() + ") " + "records";
    cnn.Close();
}

 

Posted one year ago
AnkitPal says:
SqlCommand cmd = new SqlCommand("SELECT COUNT(*) column_name FROM table_name", cnn);
            {
                cnn.Open();
                SqlDataReader rdr = cmd.ExecuteReader();
                int count = 0;
                while (rdr.Read())
                {
                    count++;
                }
                label1.Text = "(" +count.ToString() + ") " + "records";
                cnn.Close();
            }

 replace above code with below

SqlCommand cmd = new SqlCommand("SELECT COUNT(*) Total_Count FROM table_name", cnn);
{
    cnn.Open();
    SqlDataReader rdr = cmd.ExecuteReader();
    while (rdr.Read())
    {
        label1.Text = "(" + rdr["Total_Count"].ToString() + ") " + "records";
    }
            
    cnn.Close();
}

 

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html