Line error at position 0

I've been doing research on this topic and came across many questions about this already asked, but this is a 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();

      

Messagebox returns 1. So this means strings are returned in the function. But as soon as I click Okay to continue after the message, I get this error:

IndexOutOfRangeException No row at position 0

I don't know why it displays an error on the second line and not on the first, but I tried to display the value [0] [0], but it still returns an error for the line at position 0, is it possible that the line might be in another position?

+3


source to share


1 answer


From your code, it looks like you are using an instance of the class DataTable

as the owner of the entity data. I don't think you need to use DataTable

at all. Why don't you have a class Computer

that will have fields that you will then display from the stored procedure. Something like that:

    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 the results from a stored procedure to a code object is automated by many ORMs. Dapper is a good example and is pretty easy to use.

0


source







All Articles