简体   繁体   中英

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:

    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:

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. But as soon as i press Okay to continue after the messagebox, I get this error:

IndexOutOfRangeException No Row at position 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?

From your code it looks like you are using DataTable class instance as an entity data holder. I don't think you need to use DataTable at all. Why don't you have a class Computer that will have fields you will then map from the stored procedure. 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. Dapper is a good example, and pretty easy to use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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