Show the result of a SQL Server 2008 sp in a web form C # (Visual Studio)

0

Good day everyone. I have a sp SQL Server 2008 that throws me the following result:

Total_Registros
       1

On the other hand, I have a form that contains a label (lbl_totals),

nbsp; 
<asp:Label ID="lbl_totales" runat="server" Text=""></asp:Label>

which I would like you to show the result of this sp. The sp such as, throws me the result indicated above. I have this code which I am testing, however, it still does not show the result in my label. Does anyone know where my error is? Or could you give me some additional idea?

  protected void TotalReg()
    {

        clsConexioncs cnx = new clsConexioncs();
        try
        {
            DataSet ds;
            cnx.Conexion = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultPRM"].ToString();
            cnx.PreparaComandoSP("prm_spTotalRegistrosNAV");
            ds= cnx.EjecutaComandoDataSet();


            foreach(DataRow dr in ds.Tables[0].Rows )
            {
                //lbl_totales.Text = dr[0].ToString();
                lbl_totales.Text = dr["Total_Registros"].ToString();
            }


        }
        catch (Exception ex)
        {
            MessageBox("Ha ocurrido el siguiente error " + ex.Message.ToString());
        }
        finally { cnx = null; }
    }

Expected result would be the following:

Total_registros:  [lbl_totales] 

Where lbl_totals would be the label where it would show the result of the sp. Staying in the following way:

Total_registros: 0
    
asked by Ric_hc 01.12.2017 в 19:38
source

1 answer

0

Good afternoon if you want to know the number of records returned in the query and you do not have to do the foreach just enough with the following.

lbl_totales.Text = ds.Tables.[0].Rows.Count.toString();

Now if you want to get the total sum of the Total_Registros field, you should do the following

int sumaTotal
foreach(DataRow dr in ds.Tables[0].Rows )
{
    sumaTotal += Convert.toInt32(dr["Total_Registros"]);
}
lbl_totales.Text = sumaTotal.toString();

Although this could be done in your process prm_spTotalRecordsNAV

Greetings.

    
answered by 01.12.2017 в 22:00