简体   繁体   English

DataReader索引超出数组范围

[英]DataReader index outside bounds of Array

I've the following method pulling information from a table; 我有以下方法从表中提取信息;

public Customer GetCustomerDetails(int customerID)
    {
        Customer currentCustomer = null;
        try
        {
            using (cxn = new SqlConnection(this.ConnectionString))
            {
                using (cmd = new SqlCommand("spGetCustomerInformation", cxn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@CustomerID", SqlDbType.Int).Value = customerID;
                    cxn.Open();
                    SqlDataReader dataReader = cmd.ExecuteReader();

                    currentCustomer = new Customer();
                    currentCustomer.CustomerID = customerID;

                    while (dataReader.Read())
                    {
                        currentCustomer.FirstName = dataReader.GetValue(Convert.ToInt32(CustomerTableColumns.firstName)).ToString();
                        currentCustomer.Surname = dataReader.GetValue(Convert.ToInt32(CustomerTableColumns.surname)).ToString();
                        currentCustomer.Email = dataReader.GetValue(Convert.ToInt32(CustomerTableColumns.email)).ToString();
                        currentCustomer.Phone = dataReader.GetValue(Convert.ToInt32(CustomerTableColumns.phone)).ToString();
                        currentCustomer.AddressLine1 = dataReader.GetValue(Convert.ToInt32(CustomerTableColumns.addressLine1)).ToString();
                        currentCustomer.AddressLine2 = dataReader.GetValue(Convert.ToInt32(CustomerTableColumns.addressLine2)).ToString();
                        currentCustomer.City = dataReader.GetValue(Convert.ToInt32(CustomerTableColumns.city)).ToString();
                        currentCustomer.County = dataReader.GetValue(Convert.ToInt32(CustomerTableColumns.county)).ToString();
                    }
                    dataReader.Close();
                    cxn.Close();
                }
            }
        }
        catch (SqlException)
        {
            throw;
        }

        return currentCustomer;
    }

However I keep getting the error "An unhandled exception of type 'System.IndexOutOfRangeException' occurred in System.Data.dll 但是,我不断收到错误“ System.Data.dll中发生了'System.IndexOutOfRangeException类型的未处理的异常”

Additional information: Index was outside the bounds of the array." on the last line in the while loop. 附加信息:索引在while循环的最后一行上。”

My SQL query looks like this: 我的SQL查询如下所示:

ALTER PROC [dbo].[spGetCustomerInformation]
@CustomerID INTEGER
AS 
BEGIN
SET NOCOUNT ON
SELECT
a.Firstname,
a.Surname,
a.Email,
a.Phone,
a.AddressLine1,
a.AddressLine2,
a.City,
County
FROM 
tblCustomers a
WHERE
a.CustomerID = @CustomerID
END

What is in the CustomerTableColumns, could you show us the code? CustomerTableColumns中的内容是什么,您可以向我们显示代码吗?

At first guess, I would say that the first value of your enum CustomerTableColumns is 1 to 8 and the datareader would expect to be 0 to 7 乍一看,我想说的是您的枚举CustomerTableColumns的第一个值是1到8,而datareader期望是0到7

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

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