How can I delete a line from a Session?

Asked

Viewed 134 times

-1

I have a NO DATABASE ENTERED page, the registration data is being stored in Session. I am using a Reset to store this. The delete command that I used before was through Reset, which is wrong because it has to be done by Session otherwise the operation is not correct. How can I do that? I’ve tried Session.Abandon; Session.Remove; Session.Removeat I’m starting to use c# Asp.net now

                    <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
                        <HeaderTemplate>
                            <tr>
                                <td>Ações:</td>
                                <td>Nome:</td>
                                <td> ....</td>
                            </tr>
                        </HeaderTemplate>
                        <ItemTemplate > 
                            <tr>
                                <td>
                                    <asp:button ID="btnAlterar" runat="server" CommandName="Alterar" OnCommand="btnAlterar_Click" text="Alterar"></asp:button>
                                    <asp:button ID="btnExcluir" runat="server" CommandName="Excluir" OnCommand="btnExcluir_Click" text="Excluir"></asp:button>
                                </td> 
                                <td>
                                    <%# DataBinder.Eval(Container.DataItem, "Nome") %>
                                </td>
                                <td>
                                    <%# DataBinder.Eval(Container.DataItem, "Email") %>
                                </td>
                                <td>
                                    <%# DataBinder.Eval(Container.DataItem, "Nasci") %>
                                </td>
                                <td>
                                    <%# DataBinder.Eval(Container.DataItem, "Cpf") %>
                                </td>
                                <td>
                                    <%# DataBinder.Eval(Container.DataItem, "Cidade") %>
                                </td>
                                <td>
                                    <%# DataBinder.Eval(Container.DataItem, "Estado") %>
                                </td>
                                <td>
                                    <%# DataBinder.Eval(Container.DataItem, "Endereco") %>
                                </td>
                                <td>
                                    <%# DataBinder.Eval(Container.DataItem, "Num") %>
                                </td>
                            </tr>
                        </ItemTemplate>    
                    </asp:Repeater>  
    </table>//Table onde está o Repeater e os Botões 

Repeater itemCommand:

protected void Repeater1_itemcommand(Object source, Repeatercommandeventargs and) {

    switch (e.CommandName)
    {
        case "Alterar":
            break;


        case "Excluir":
            Session.Contents.Remove("lst");

            break;
    }
}

This is how data is being stored in Session:

Person p = new Person();

    p.Nome = txtNome.Text;
    p.Email = txtEmail.Text;
    p.Nasci = txtNasci.Text;
    p.Cpf = txtCpf.Text;
    p.Cidade = txtCidade.Text;
    p.Estado = ListEstado.Text;
    p.Endereco = txtEndereco.Text;
    p.Num = Repeater1.Items.Count;

    if (Session["lst"] == null)
    {
        List<Pessoa> lstNova = new List<Pessoa>();
        lstNova.Add(p);

        Session["lst"] = lstNova;

        Repeater1.DataSource = lstNova;
        Repeater1.DataBind();
    }
    else
    {
        List<Pessoa> lstTodoMundo = (List<Pessoa>)Session["lst"];
        lstTodoMundo.Add(p);
        Repeater1.DataSource = lstTodoMundo;
        Repeater1.DataBind();

    }

1 answer

0


One Session stores an object, and in its case stores a List. You will not be able to change any list value using Session.Removeat for example, why there you will be removing the object that is in the Session (which is the list) and nay an item from the list.

Soon, you will first have to delete from list and then update the Session with the updated list.

1. Identify the record of redepeater that you want to delete/update by placing a value for the Commandargument. I used the "Num" property of the object Person.

<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
            <HeaderTemplate>
                <tr>
                    <td>Ações:</td>
                    <td>Nome:</td>
                    <td>...</td>
                </tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td>
                        <asp:Button ID="btnAlterar" runat="server" CommandName="Alterar" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Num") %>' OnCommand="btnAlterar_Command" Text="Alterar"></asp:Button>
                        <asp:Button ID="btnExcluir" runat="server" CommandName="Excluir" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Num") %>'  OnCommand="btnExcluir_Command" Text="Excluir"></asp:Button>
                    </td>
                    <td>
                        <%# DataBinder.Eval(Container.DataItem, "Nome") %>
                    </td>
                    <td>
                        <%# DataBinder.Eval(Container.DataItem, "Email") %>
                    </td>
                    <td>
                        <%# DataBinder.Eval(Container.DataItem, "Nasci") %>
                    </td>
                    <td>
                        <%# DataBinder.Eval(Container.DataItem, "Cpf") %>
                    </td>
                    <td>
                        <%# DataBinder.Eval(Container.DataItem, "Cidade") %>
                    </td>
                    <td>
                        <%# DataBinder.Eval(Container.DataItem, "Estado") %>
                    </td>
                    <td>
                        <%# DataBinder.Eval(Container.DataItem, "Endereco") %>
                    </td>
                    <td>
                        <%# DataBinder.Eval(Container.DataItem, "Num") %>
                    </td>
                </tr>
            </ItemTemplate>
        </asp:Repeater>
  1. At the event Repeater1_itemcommand: recover the "num" of Person received in the parameter e. Commandargument. Then find the object Person on the list (which is already on the Session) through "Num". Remove the object from the list Person found and update to Session with the updated list.

    protected void Repeater1_itemcommand(Object source, Repeatercommandeventargs and) { switch (e.Commandname) { case "Change": break; case "Delete":

                int numero = 0;
                int.TryParse(e.CommandArgument.ToString(), out numero);
    
                var lstPessoas = (List<Pessoa>)Session["lst"];
    
                // Não se esqueça de importar o namespace System.Linq para usar o Where na lista
                // Importar: using System.Linq; 
                var pessoa = lstPessoas.Where(p => p.Num == numero).FirstOrDefault();
    
                lstPessoas.Remove(pessoa);
    
                Session["lst"] = lstPessoas;
    
                Repeater1.DataSource = lstPessoas;
                Repeater1.DataBind();
    
                break;
        }
    }
    
  • I only managed to perform the tests today...but thank you so much is working properly! Thank you so much :))

  • Not at all @Gustavoluiz. See you around ;)

Browser other questions tagged

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