C # - Use form data in IF

2

I'm trying to retrieve data from the form and need to use it in an IF.

protected void btnSubmit_Click(object sender, EventArgs e)
{
    try
    {
        if (txtLocalizacao.Text != "")
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand("INSERT INTO Equipamento (IDCliente,Localizacao,IDServidor,Porta,Atualizar,EnviarMensagem,IDTipoEquipamento) VALUES (@IDCliente,@Localizacao,@IDServidor,@Porta,1,@EnviarMensagem, @IDTipoEquipamento);SELECT CAST(scope_identity() AS int);", conn); 
            cmd.Parameters.AddWithValue("@IDCliente", IDCliente);
            cmd.Parameters.AddWithValue("@Localizacao", txtLocalizacao.Text);
            cmd.Parameters.AddWithValue("@IDServidor", ddlServidor.SelectedValue);
            cmd.Parameters.AddWithValue("@IDTipoEquipamento", ddlTipoEqp.SelectedValue);
            cmd.Parameters.AddWithValue("@Porta", txtPorta.Text);
            cmd.Parameters.AddWithValue("@EnviarMensagem", chkEnviarMensagem.Checked);
            Int32 intIDEquipCadastrado = (Int32)cmd.ExecuteScalar();
            cmd.CommandText = "INSERT INTO EquipamentoEstadoAtual (IDEquipamento,IDCor,DataHoraAtualizacao) VALUES (" + intIDEquipCadastrado .ToString() + ",4,GETDATE())";
            cmd.ExecuteNonQuery();

            if (@IDTipoEquipamento == 1)
            { 
                cmd.CommandText = "INSERT INTO EntradaEstado (IDEquipamento,IDEntradaTipo,IDEntrada, Valor, DataHoraAtualizacao) VALUES (" + intIDEquipCadastrado.ToString() + ",1,0,0,GETDATE()),(" + intIDEquipCadastrado.ToString() + ",1,1,0,GETDATE()),(" + intIDEquipCadastrado.ToString() + ",1,2,1,GETDATE()),(" + intIDEquipCadastrado.ToString() + ",1,3,0,GETDATE()),(" + intIDEquipCadastrado.ToString() + ",1,4,0,GETDATE()),(" + intIDEquipCadastrado.ToString() + ",1,5,0,GETDATE()),(" + intIDEquipCadastrado.ToString() + ",1,6,0,GETDATE()),(" + intIDEquipCadastrado.ToString() + ",1,7,1,GETDATE());";

            } else if(@IDTipoEquipamento == 2){
                cmd.CommandText = "INSERT INTO EntradaEstado (IDEquipamento,IDEntradaTipo,IDEntrada, Valor, DataHoraAtualizacao) VALUES (" + intIDEquipCadastrado.ToString() + ",1,8,0,GETDATE()), (" + intIDEquipCadastrado.ToString() + ",1,9,0,GETDATE()), (" + intIDEquipCadastrado.ToString() + ",1,10,1,GETDATE()), (" + intIDEquipCadastrado.ToString() + ",1,11,0,GETDATE()), (" + intIDEquipCadastrado.ToString() + ",1,12,0,GETDATE());";
            }

            cmd.ExecuteNonQuery();
            cmd.Dispose();
            //Response.Redirect("default.aspx");
            //ShowMessage("E-mail cadastrado com sucesso!");
            clear();
            BindGridView();
        }
    }
    catch (SqlException ex)
    {
        ShowMessage(ex.Message);
    }
    finally
    {
        conn.Close();
    }
}

I believe that retrieving the information in if through the @IDTypeEquipment is wrong. How could I recover this information from my if?

    
asked by anonymous 30.06.2017 / 21:19

1 answer

1

Change your if with the following:

if (ddlTipoEqp.SelectedValue == 1)
{ 
    cmd.CommandText = "INSERT INTO EntradaEstado (IDEquipamento,IDEntradaTipo,IDEntrada, Valor, DataHoraAtualizacao)
    VALUES (" + intIDEquipCadastrado.ToString() + ",1,0,0,GETDATE()),(" +
    intIDEquipCadastrado.ToString() + ",1,1,0,GETDATE()),(" +
    intIDEquipCadastrado.ToString() + ",1,2,1,GETDATE()),(" +
    intIDEquipCadastrado.ToString() + ",1,3,0,GETDATE()),(" +
    intIDEquipCadastrado.ToString() + ",1,4,0,GETDATE()),(" +
    intIDEquipCadastrado.ToString() + ",1,5,0,GETDATE()),(" +
    intIDEquipCadastrado.ToString() + ",1,6,0,GETDATE()),(" +
    intIDEquipCadastrado.ToString() + ",1,7,1,GETDATE());";
} 
else if(ddlTipoEqp.SelectedValue == 2)
{
    cmd.CommandText = "INSERT INTO EntradaEstado (IDEquipamento,IDEntradaTipo,IDEntrada, Valor, DataHoraAtualizacao)
    VALUES (" + intIDEquipCadastrado.ToString() + ",1,8,0,GETDATE()), (" +
    intIDEquipCadastrado.ToString() + ",1,9,0,GETDATE()), (" +
    intIDEquipCadastrado.ToString() + ",1,10,1,GETDATE()), (" +
    intIDEquipCadastrado.ToString() + ",1,11,0,GETDATE()), (" +
    intIDEquipCadastrado.ToString() + ",1,12,0,GETDATE());";
}

So you will compare the value that was previously registered in your Equipment table. You can even create a variable and pass it the ddlTipoEqp.SelectedValue value.

    
30.06.2017 / 22:02