Gurjeet Singh

Gurjeet Singh

  • NA
  • 90
  • 95k

Count Rows

Aug 9 2011 6:51 AM
Hi i want to show total rows in my HR table on label. I have wrote below code in App_Code\Class5
which code should i used in Label.Text property.




using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Data.SqlTypes;

public class Class5
{
  private static readonly string Con_String;
  public int result=0;
 
  private int CountRows()
  {
              SqlConnection con = new SqlConnection(Con_String);
  SqlCommand cmd = new SqlCommand("StoredProcedure1",con);
  cmd.CommandType = CommandType.StoredProcedure;

  cmd.Parameters.Add("@ReturnVal", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
  using (con)
  {
  con.Open();
  cmd.ExecuteNonQuery();
  result=(int)cmd.Parameters["@ReturnVal"].Value;
  }
  return result;
  }

  static Class5()
  {
  Con_String = WebConfigurationManager.ConnectionStrings["Example"].ConnectionString;
  }
}

 

StoredProcedure


CREATE PROCEDURE dbo.StoredProcedure1
AS
RETURN (select count(*) from HR)



Answers (1)