Manipulating related data Asp.net and C#

Asked

Viewed 167 times

2

I am developing an ASP.NET application, which aims to register events and add customers to these registered events, forming a customer list for each event.

My question is this: In the Events table there are three types of entry (whole type - because it keeps the amount of entry available for such event), which are Courtesy, Bonus and Vip. So far so good. When adding a new customer for a particular event, I need to select his or her input type on a radio button (Courtesy, Bonus and Vip). Whenever I add a customer, I need the amount of Events input type to decrease 1.
What I’m not getting is to recover the value of the selected input type on the radiobutton to do the data manipulation.

OBS: In the form to include client I select in a dropdownlist the desired event. I don’t know if you need this information or if through it I can recover the values of each type of input.

It seems simple, but since I’m at the beginning, I’m stuck at this point.

protected void btnIncluir_Click(object sender, EventArgs e)
{
    var db = new TheOneWebEntities();
    var cliente = new Cliente();
    cliente.Nome = tbNome.Text;
    cliente.Cpf = tbCpf.Text;
    cliente.Email = tbEmail.Text;
    cliente.Telefone = tbTelefone.Text;
    cliente.EventoId = Convert.ToInt32(ddlEvento.SelectedValue);

    int totalEntrada;

    if (rbCortesia.Checked)
    {
        if (evento.Cortesia <= 0)
        {
            Util.Alertar("Cortesia esgotada");
        }
        else
        {
            totalEntrada = cliente.Evento.Cortesia - 1;
            cliente.Evento.Cortesia = totalEntrada;
        }
    }
    else
    {
        if (rbBonus.Checked)
        {
            if (evento.Bonus <= 0)
            {
                Util.Alertar("Bônus esgotado");
            }
            else
            {
                totalEntrada = cliente.Evento.Bonus - 1;
                cliente.Evento.Bonus = totalEntrada;
            }
        }
        else
        {
            if (rbVip.Checked)
            {
                if (evento.Vip <= 0)
                {
                    Util.Alertar("VIP esgotado");
                }
                else
                {
                    totalEntrada = cliente.Evento.Vip - 1;
                    cliente.Evento.Vip = totalEntrada;
                }
            }
        }
    }
    var dataCadastro = DateTime.Now.ToString();
    cliente.DataCadastro = Convert.ToDateTime(dataCadastro);
    cliente.UsuarioId = WebMatrix.WebData.WebSecurity.CurrentUserId;

    db.Clientes.Add(cliente);
    db.SaveChanges();
    Util.Alertar("Cliente adicionado com sucesso!");
    Response.Redirect("~/User/MeusClientes.aspx");
}

2 answers

3

Just complementing the Gypsy response

create Radiobuttonlist that way:

        <asp:RadioButtonList ID="rdgTipoEntrada" runat="server">
            <asp:ListItem Value="Cortesia">Cortesia</asp:ListItem>
            <asp:ListItem Value="Bonus">Bônus</asp:ListItem>
            <asp:ListItem Value="VIP">Vip</asp:ListItem>
        </asp:RadioButtonList>

Codebehind

switch (rdgTipoEntrada.SelectedValue)
        {
            case "Cortesia":

                if (evento.Cortesia <= 0)
                {
                    Util.Alertar("Cortesia esgotada");
                }
                else
                {
                    totalEntrada = cliente.Evento.Cortesia - 1;
                    cliente.Evento.Cortesia = totalEntrada;
                }

                break;
            case "Bonus":

                if (evento.Bonus <= 0)
                {
                    Util.Alertar("Bônus esgotado");
                }
                else
                {
                    totalEntrada = cliente.Evento.Bonus - 1;
                    cliente.Evento.Bonus = totalEntrada;
                }

                break;
            case "VIP":

                if (rbVip.Checked)
                {
                    if (evento.Vip <= 0)
                    {
                        Util.Alertar("VIP esgotado");
                    }
                    else
                    {
                        totalEntrada = cliente.Evento.Vip - 1;
                        cliente.Evento.Vip = totalEntrada;
                    }
                }


                break;

        }

2

If it is a RadioButtonList, I think it is easier to give the value by own RadioButtonList:

switch (rdgTipoEntrada.SelectedItem.Value.ToString())
{
    case "Cortesia":
       ...
    case "Bônus":
       ...
    case "VIP":
       ...
}

Browser other questions tagged

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