简体   繁体   English

位置0处没有行错误

[英]No row at position 0 error

I did research on the topic and came across a lot of questions about this one already asked, but this is different scenario. 我对这个主题进行了研究,并且遇到了很多关于这个问题的问题,但这是不同的情况。

I have the following Code in my dbConnection Class: 我的dbConnection类中有以下代码:

    public void GetCertainComputer(string ComputerBarcode)
    {

        cnn.Open();
        tbl = new DataTable();
        cmd = new SqlCommand(String.Format("exec ComputerInsertUpdateDelete 4, @CompBarcode = '{0}'", ComputerBarcode), cnn);
        reader = cmd.ExecuteReader();

        for (int i = 0; i < reader.FieldCount; i++)
        {
            tbl.Columns.Add(reader.GetName(i));
        }

        while (reader.Read())
        {
            row = tbl.NewRow();

            for (int i = 0; i < reader.FieldCount; i++)
            {
                row[i] = reader[i];
            }

            tbl.Rows.Add(row);
        }

        cnn.Close();
        reader.Close();
        cmd.Dispose();
    }

And in my MainWindow I call this function: 在我的MainWindow中,我调用了这个函数:

db.GetCertainComputer(DGMain.SelectedValue.ToString());

System.Windows.MessageBox.Show(db.tbl.Rows.Count.ToString()); //Row count for Debugging.

txtBarcodeUpd_Computer.Text = db.tbl.Rows[0][0].ToString();
txtDescriptionUpd_Computer.Text = db.tbl.Rows[0][1].ToString();
cbMouseUpd_Computer.SelectedValue = db.tbl.Rows[0][3].ToString();
cbMonitorUpd_Computer.SelectedValue = db.tbl.Rows[0][4].ToString();
cbKeyboardUpd_Computer.SelectedValue = db.tbl.Rows[0][5].ToString();
cbSupplierUpd_Computer.SelectedValue = db.tbl.Rows[0][6].ToString();
cbCourseUpd_Computer.SelectedValue = db.tbl.Rows[0][7].ToString();
cbBranchUpd_Computer.SelectedValue = db.tbl.Rows[0][8].ToString();

The Messagebox returns a value of 1. So it means there are rows returned with the function. Messagebox返回值1.因此,这意味着函数返回了行。 But as soon as i press Okay to continue after the messagebox, I get this error: 但是,只要我按下Okay继续消息框后,我就会收到此错误:

IndexOutOfRangeException No Row at position 0 IndexOutOfRangeException位置0处没有行

I do not know why It displays the error on the second line and not the first, but I tried displaying the [0][0] value also, but still returns and error for no row at position 0, Is it possible that the row could be at another position? 我不知道为什么它在第二行而不是第一行显示错误,但我也尝试显示[0] [0]值,但仍然返回并且错误为0位没有行,是否可能是行可能在另一个位置?

From your code it looks like you are using DataTable class instance as an entity data holder. 从您的代码看起来您​​使用DataTable类实例作为实体数据持有者。 I don't think you need to use DataTable at all. 我认为你根本不需要使用DataTable Why don't you have a class Computer that will have fields you will then map from the stored procedure. 为什么没有一个类Computer ,它将具有您将从存储过程映射的字段。 Something like this: 像这样的东西:

    public class Computer {
    public string <or whatever type it is> Keyboard {get; set;}
        ...
    }

    public IEnumerable<Computer> GetComputers(string ComputerBarcode)
    {
        var computers = new List<Computer>();
        cnn.Open();
        tbl = new DataTable();
        cmd = new SqlCommand(String.Format("exec ComputerInsertUpdateDelete 4, @CompBarcode = '{0}'", ComputerBarcode), cnn);
        reader = cmd.ExecuteReader();



        while (reader.Read())
        {
           computers.Add(new Computer(){ 
                   Keyboard = reader["keyboard"<or whatever your field name for keyboard is>]
               });
        }

        cnn.Close();
        reader.Close();
        cmd.Dispose();

        return computers;
    }

This manual work of mapping of results from stored procedure to a code object is automated by many ORM's. 这种从存储过程到代码对象的结果映射的手动工作由许多ORM自动完成。 Dapper is a good example, and pretty easy to use. Dapper是一个很好的例子,非常容易使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM