ListBox elements are duplicated when calling the selection of an index

181 Views Asked by At

Problem:

I have a listbox that i fill with a dataset in that dataset i have row nombre and row cod_seg. I assign row nombre to the property display member and the cod_seg to the property display member. In the selectindexchange event of that listbox i create another dataset that bring me data that depends of the selectvalue of the listbox that happens without problem, the problem is that when i select one item of the listbox it duplicate the tem select in the listbox.

Dont know why this is happening and i check my code 10 times...

Code: Fill Listbox

private void FillData(int cs)
{
    CS = cs;


    ds.Clear();

    string cmd = "select CompaniaSeguro.Nombre,CompaniaSeguro.Telefono,CompaniaSeguro.Email from CompaniaSeguro where CompaniaSeguro.Cod_Comp = " + cs.ToString();
    ds = mytool.Execute(cmd);
    if (ds.Tables[0].Rows.Count > 0)
    {
        txtNC.Text = ds.Tables[0].Rows[0]["Nombre"].ToString();

        if (ds.Tables[0].Rows[0]["Telefono"].ToString().Trim().Length == 0)
            txtTelefono.Text = "No Asignado";
        else
            txtTelefono.Text = ds.Tables[0].Rows[0]["Telefono"].ToString();


        if (ds.Tables[0].Rows[0]["Email"].ToString().Trim().Length == 0)
            txtEmail.Text = "No Asignado";
        else
            txtEmail.Text = ds.Tables[0].Rows[0]["Email"].ToString();



    }

    ds.Clear();


    cmd = "select  Seguro.Cod_Seg , Seguro.Nombre from Seguro where Seguro.Cod_Comp = " + cs.ToString();
    ds = mytool.Execute(cmd);
    if (ds.Tables[0].Rows.Count > 0)
    {
        listBox1.DataSource = ds.Tables[0];
        listBox1.DisplayMember = "Nombre";
        listBox1.ValueMember = "Cod_Seg";
        finish = true;

    }

}

Code: Listbox selected index changed

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{

    if (finish == true)
    {
        if (listBox1.SelectedValue != null)
        {
            DataSet myds = new DataSet();
            codS = listBox1.SelectedValue.ToString();
            string cmd = "select Seguro.Nombre,Seguro.Descuento from Seguro where Seguro.Cod_Seg = " + codS;
            myds = mytool.Execute(cmd);

            if (ds.Tables[0].Rows.Count > 0)
            {
                MessageBox.Show("trae");
            }


        }
    }

}

Image

Image

0

There are 0 best solutions below