SidraM
SidraM

Reputation: 305

How to show number of rows return in select statement in asp.net

I am fetching records of users and binding it to gridview. I want to show the number of results returned like "15 results found". I used RecordsAffected but I think it wont work with select statement. Any alternate way?

using (SqlConnection con = new SqlConnection(strCon))
{
    using (SqlCommand cmd = new SqlCommand())
    {

        cmd.CommandText = "select name, city, number where age between " + from.Text + "AND " + to.Text;
        cmd.Connection = con;
        con.Open();
        SqlDataReader reader = cmd.ExecuteReader();
        reader.Read();

        if (reader.HasRows)
        {
            reader.Close();
            usersgrid.DataSource = cmd.ExecuteReader();
            usersgrid.DataBind();
            con.Close();

        }
        else
        {
            result.Visible = true;
        }
    }
}

Now I want to show number of rows returned on a label.

Upvotes: 0

Views: 1128

Answers (1)

Nahum Fabian
Nahum Fabian

Reputation: 180

int affectedRows = cmd.ExecuteNonQuery();

that would work for you insted of cmd.ExecuteReader();

another alternative will be to get rowcount of the usersGrid control

int Count = usersGrid.Rows.Count-((usersGrid.PageCount-1) * usersGrid.PageSize);

Upvotes: 2

Related Questions