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.