计算具有值的变量,如果满足条件,请执行我想要的操作

时间:2018-11-16 13:10:40

标签: c# asp.net

当输入框具有值时,我试图使它允许用户将数据提交到数据库。输入框和下拉列表值必须大于零的地方。如果if条件中的值大于零,则更新数据库表并插入if语句中提供的数据。我想要一个类似int count的东西,它可以计算多少条件被大于0的值填充,它可以让用户提交数据。那就是我所缺少的,无法弄清楚该怎么做。这是我的代码:

int ContratoProyeccionID = (int)gvContratoProyecciones.SelectedValue;
        try
        {
            int razon = 0;

            if (int.TryParse(txtTotalHoras.Value, out razon) && razon > 0)
            SometerHoras(ContratoProyeccionID, decimal.Parse(txtTotalHoras.Value), 0);

            if (ddlRazon1.SelectedIndex > 0 && int.TryParse(txtHorasRazon1.Value, out razon) && razon > 0) 
                SometerHoras(ContratoProyeccionID, decimal.Parse(txtHorasRazon1.Value), ddlRazon1.SelectedIndex);

            if (ddlRazon2.SelectedIndex > 0 && int.TryParse(txtHorasRazon2.Value, out razon) && razon > 0)
                SometerHoras(ContratoProyeccionID, decimal.Parse(txtHorasRazon2.Value), ddlRazon2.SelectedIndex);

            if (ddlRazon3.SelectedIndex > 0 && int.TryParse(txtHorasRazon3.Value, out razon) && razon > 0)
                SometerHoras(ContratoProyeccionID, decimal.Parse(txtHorasRazon3.Value), ddlRazon3.SelectedIndex);

            if (ddlRazon4.SelectedIndex > 0 && int.TryParse(txtHorasRazon4.Value, out razon) && razon > 0)
                SometerHoras(ContratoProyeccionID, decimal.Parse(txtHorasRazon4.Value), ddlRazon4.SelectedIndex);

            if (ddlRazon5.SelectedIndex > 0 && int.TryParse(txtHorasRazon5.Value, out razon) && razon > 0)
                SometerHoras(ContratoProyeccionID, decimal.Parse(txtHorasRazon5.Value), ddlRazon5.SelectedIndex);

            //Hacer Update a Estatus 
            SqlHelper objHelper = new SqlHelper();
            objHelper.Timeout = 5000;

            using (SqlConnection sCon2 = new SqlConnection(objHelper.ConnectionString))
            {
                sCon2.Open();
                SqlTransaction sTran2 = sCon2.BeginTransaction();
                ContratoProyeccion CP = ContratoProyeccion.Get(contratoproyeccionID: ContratoProyeccionID).First();
                CP.Estatus = "R";
                ContratoProyeccion.Update(sCon2, sTran2, CP);

                sTran2.Commit();
                      }

            pnlRegistro.Visible = false;
            ddlRazon1.SelectedIndex = 0;
            ddlRazon2.SelectedIndex = 0;
            ddlRazon3.SelectedIndex = 0;
            ddlRazon4.SelectedIndex = 0;
            ddlRazon5.SelectedIndex = 0;
            txtTotalHoras.Value = "";
            txtHorasRazon1.Value = "";
            txtHorasRazon2.Value = "";
            txtHorasRazon3.Value = "";
            txtHorasRazon4.Value = "";
            txtHorasRazon5.Value = "";

            string message = "Registro de Asistencia Exitoso";
            this.AlertBoxMessage.InnerText = message;
            this.AlertBox.Visible = true;
        }
        catch (Exception exC)
        {

            throw new Exception("Error sometiendo horas de contrato " + ContratoProyeccionID, exC);

        }
    }

1 个答案:

答案 0 :(得分:1)

这样做

    int counter;
    try
    {
        int razon = 0;
        counter = 0

        if (int.TryParse(txtTotalHoras.Value, out razon) && razon > 0)
        {
          SometerHoras(ContratoProyeccionID, decimal.Parse(txtTotalHoras.Value), 0); 
          counter ++;
        }

ecc .... 如果需要,则将counter ++放进去,然后就完成了