0

I have a combox box that i fill with the data my database , i call the FillCombos function in the constructor of the form.

//Variables of class

 DataSet ds = new DataSet();
 DataTools mytool = new DataTools();

private void FillCombos()
    {
        string cmd = "";

        //Llena cmb Categoria
        ds.Clear();
        cmd = "select descripcion from Categoria";
        ds = mytool.Execute(cmd);

        if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                this.cmbCategoria.Items.AddRange(new object[] 
                  {
                    ds.Tables[0].Rows[i]["descripcion"].ToString()});
        }
        ds.Clear();
     }

Then when the user select a category i want to display the subcategories. So i have this method.

private void cmbCategoria_SelectedIndexChanged(object sender, EventArgs e)
    {
        //ds.Clear();
        string cmd = "select Categoria.cod_cat from Categoria where Categoria.descripcion = '" + cmbCategoria.Text + "'";
        ds = mytool.Execute(cmd);

    //Keeps telling me that is out of range here is the problem
        MessageBox.Show(ds.Tables[0].Rows[0][0].ToString());
        if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            MessageBox.Show(cmd);
            MessageBox.Show(ds.Tables[0].Rows[0][0].ToString());
        }
       // codCat = Convert.ToInt32(ds.Tables[0].Rows[0]["cod_cat"].ToString());
        ds.Clear();

        cmd = "select descripcion  from SubCategoria where cod_cat = " + codCat.ToString();
        ds = mytool.Execute(cmd);

        this.cmbSubCategoria.Items.Clear();
        if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                this.cmbSubCategoria.Items.AddRange(new object[] 
                  {
                    ds.Tables[0].Rows[i]["descripcion"].ToString()});
        }
        ds.Clear();
    }

My problem is that visual studio tells me that the dataset it out of range, but i try the sentence in sql and it returns one id with one row and one column, so why the message that the index is out of range, why the sql statement is not executed correctly.

I have the same code in a adress form and it runs correctly. I try changing the dataset, another datool class with another dataset, putting a static sql query but nothing seems to work.

  • 1
    So you have set a breakpoint at `//Keeps telling me that is out of range...` and looked at `ds.Table[0]`? Does it really contain rows? – Tim Schmelter Aug 25 '14 at 10:51
  • I try using a new dataset and class mytool again, and now it seems to work, i dont understand why the change and that's what worries me more – Sahari Witbite Aug 25 '14 at 10:57

1 Answers1

0

The SELECT statement is very probably returning no results as cmbCategoria.Text is not containing the expected value. Please stop your pgm before executing the SELECT statement and have a look to the content of cmbCategoria.Text

Update: Try instead string cmd = "select Categoria.cod_cat from Categoria where Categoria.descripcion = '" + cmbCategoria.SelectedItem.ToString() + "'";

More info here: Combobox.Text Vs combobox.Selecteditem Vs combobox.selectValue?

Community
  • 1
  • 1
Guido
  • 926
  • 2
  • 10
  • 19