Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 数据集中的索引超出范围,但sql语句正确_C#_Sql_Sql Server_Winforms_Dataset - Fatal编程技术网

C# 数据集中的索引超出范围,但sql语句正确

C# 数据集中的索引超出范围,但sql语句正确,c#,sql,sql-server,winforms,dataset,C#,Sql,Sql Server,Winforms,Dataset,我有一个用数据库中的数据填充的组合框,我在表单的构造函数中调用FillCombos函数 //Variables of class DataSet ds = new DataSet(); DataTools mytool = new DataTools(); private void FillCombos() { string cmd = ""; //Llena cmb Categoria ds.Clear(); c

我有一个用数据库中的数据填充的组合框,我在表单的构造函数中调用FillCombos函数

//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();
     }
//类的变量
数据集ds=新数据集();
DataTools mytool=新的DataTools();
私有void FillCombos()
{
字符串cmd=“”;
//Llena cmb分类
ds.Clear();
cmd=“从分类中选择描述”;
ds=mytool.Execute(cmd);
如果(ds.Tables.Count>0&&ds.Tables[0].Rows.Count>0)
{
对于(int i=0;i
然后,当用户选择一个类别时,我想显示子类别。所以我有这个 方法

private void CMB categoria\u SelectedIndexChanged(对象发送方,事件参数e)
{
//ds.Clear();
string cmd=“从Categoria中选择Categoria.cod_cat,其中Categoria.description='”+cmbccategoria.Text+”;
ds=mytool.Execute(cmd);
//一直告诉我这超出了范围这就是问题所在
Show(ds.Tables[0]。行[0][0]。ToString());
如果(ds.Tables.Count>0&&ds.Tables[0].Rows.Count>0)
{
MessageBox.Show(cmd);
Show(ds.Tables[0]。行[0][0]。ToString());
}
//codCat=Convert.ToInt32(ds.Tables[0].Rows[0][“cod_cat”].ToString());
ds.Clear();
cmd=“从子类别中选择描述,其中cod_cat=“+codCat.ToString();
ds=mytool.Execute(cmd);
this.cmbSubCategoria.Items.Clear();
如果(ds.Tables.Count>0&&ds.Tables[0].Rows.Count>0)
{
对于(int i=0;i
我的问题是VisualStudio告诉我数据集超出了范围,但我尝试了 sql中的语句,它返回一个id和一行一列,因此为什么索引超出范围的消息,为什么sql语句没有正确执行

我在adress表单中有相同的代码,并且运行正确。
我尝试更改dataset,将另一个datool类更改为另一个dataset,放置一个静态sql查询,但似乎什么都不起作用。

SELECT语句很可能没有返回任何结果作为CMBCataria。文本不包含预期值。请在执行SELECT语句之前停止您的pgm,并查看CMBCegoria.Text的内容

更新:改为尝试
string cmd=
“从Categoria中选择Categoria.cod_cat,其中Categoria.description=””
+cmbCategoria.SelectedItem.ToString()
+ "'";


这里的更多信息:

那么您已经在
//设置了一个断点,一直告诉我这超出了范围…
并查看了
ds.Table[0]
?它真的包含行吗?我再次尝试使用一个新的数据集和类mytool,现在它似乎可以工作了,我不明白为什么会有这样的变化,这是我更担心的
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();
    }