C# - Use form data in IF

Asked

Viewed 55 times

2

I am trying to recover data from the form and need to use them 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 recovering the information in the if through @Idtipoequipamento is wrong. How could I recover this information in my if?

  • Is your if code a sequence of the code above? Where is this if called? Could you show more details?

  • 1

    It’s the code sequence. I have an equipment register, in case the user chooses to register an equipment of Idtipoequipamento = 1 he will insert in the table Inadaestado in a way, in case he chooses a equipment of type 2 he will enter other data. I will edit the post with the code of the entire function.

  • Right, now you are better to visualize your code. I put the answer below for you, I believe it already solves your problem if you change the @IDTipoEquipamento in your if by ddlTipoEqp.SelectedValue.

1 answer

1


Exchange your if for 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 the value of ddlTipoEqp.SelectedValue.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.