Marius Vasile

Marius Vasile

  • 590
  • 1.9k
  • 147.2k

Display result of a sql server stored procedure in asp.net c#

Jan 2 2024 1:21 PM

I have a stored procedure and the result I wanted displayed on a page. The problem is that the result will not be stored as it is dynamic but calculated and displayed everytime page load and I don't know how to do that.

CREATE PROCEDURE [dbo].[NivelGlobalRisc] 

AS 

DECLARE @result decimal(6,2),@ActivitateNGR decimal(6,2),@ZonaLucruNGR decimal(6,2),@CaracterSpecialNGR decimal(6,2)

SET	@ActivitateNGR = (SELECT CONVERT(decimal(4,2), Sum(NivelRisc))/CONVERT(decimal(4,2), Count(NivelRisc)) FROM tblActivitate)
SET	@ZonaLucruNGR = (SELECT CONVERT(decimal(4,2), Sum(NivelRisc))/CONVERT(decimal(4,2), Count(NivelRisc)) FROM tblZonaLucru)
SET	@CaracterSpecialNGR = (SELECT CONVERT(decimal(4,2), Sum(NivelRisc))/CONVERT(decimal(4,2), Count(NivelRisc)) FROM tblCaracterSpecial)


SET	@result = (@ActivitateNGR + @ZonaLucruNGR + @CaracterSpecialNGR)/3;
protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
    }
    GetNGR();
}
protected void GetNGR()
{
    using (SqlConnection conn = new SqlConnection(connString))
    {
        using (SqlCommand cmd = new SqlCommand("NivelGlobalRisc", conn))
        {
            cmd.CommandType = CommandType.StoredProcedure;

            conn.Open();
            SqlDataReader rdr = cmd.ExecuteReader();

            while (rdr.Read())
            {
                txtNGR.Text = rdr["@result"].ToString();
            }
            conn.Close();
        }
    }
}

How it should be done, please?


Answers (5)