Get items from a ListString - ASP.NET

2

I'm new to developing ASP.NET with C #, and I have the following question:

When loading a page, I run a method that populates a DropDownList. This DropDownList is part of a form and after it is filled out and the user clicks on the "Include" button, the Include () method will be triggered.

My intent is to get the Index of the selected DDL option because, based on this number, I get the ID that was stored while loading the page in a List

See the code:

    List<String> _Processos = new List<String>();
    List<String> _Unidades = new List<String>();


    protected bool Incluir() 
    {
        bool r = false;

        System.Windows.Forms.MessageBox.Show(cbUnidadeIncidente.SelectedIndex.ToString());

        OC o = new OC();
        o.IdUsuario = (String)Session["IDUsuario"];
        o.Dt = DateTime.Parse(txtDtOcorrencia.Text);
        o.Descricao = Kompaktor.K.KSTR(txtDescricao.Text);
        o.UnidadeIncidente = _Unidades[cbUnidadeIncidente.SelectedIndex];
        o.UnidadeAfetada = _Unidades[cbUnidadeAfetada.SelectedIndex];
        o.Observacao = txtObservacao.Text;
        o.Processo = _Processos[cbProcesso.SelectedIndex];
        o.Perda = Kompaktor.K.KSTR(txtPerda.Text);
        o.Solucao = Kompaktor.K.KSTR(txtSolucao.Text);
        o.Incluir();

        return r;
    }

However, during execution, just the first line that tries to do the procedure of trying to get the Index triggers an error, saying that the value of the vector of the list has been exceeded.

I already know that the problem is in List < & gt ;, after all, using a messagebox, the value of the sealing is shown. Lists appear to be zeroed when loading. And now, how can I proceed (so that these lists are visible throughout the class)?

Thanks in advance!

    
asked by anonymous 27.02.2015 / 20:22

1 answer

1

I've been in doubt about your DropDownList and how you describe it in your code .. "cbProcess" .. (cb) for me is CheckBox initials ... but ... see the way you are loading DropDownList and also how you keep the data in your list.

See how you keep a Session from your list.

To load

Session["_Processos"]) = _Processos;

to recover

List<String> _Processos = Session["_Processos"]) ;

Another way would be to load your list would be like this. Exe:

private List<String> m_Processos = null;
        private List<String> Processos
        {
            get
            {
                if (m_Processos == null)
                {
                    try
                    {
                         List<String> _Processos = new List<String>(){"teste1","teste2","teste3"};

                         m_Processos = _Processos;
                    }
                    catch (Exception Exc)
                    {
                        //"lErro";
                    }
                }
                return m_Processos;
            }
        }

then just call

o.Processo = Processos[cbProcesso.SelectedIndex];

It is good for a breakpoint in your code to see what is happening ..

    
23.10.2015 / 18:16