0

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

Additional information: Index was outside the bounds of the array." on the last line in the while loop.

My SQL query looks like this:

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
7
  • Replace INTEGER with int i `procedure Commented Mar 7, 2015 at 13:19
  • use dataReader["country"] etc instead of your enum Commented Mar 7, 2015 at 13:21
  • No Joy, If i change OUTPUT i get error Msg 2715, Level 16, State 3, Procedure spGetCustomerInformation, Line 10 Column, parameter, or variable #1: Cannot find data type OUTPUT. Parameter or variable '@CustomerID' has an invalid data type. Commented Mar 7, 2015 at 13:21
  • I'll try without enum Commented Mar 7, 2015 at 13:22
  • Not related to question but if you are converting the datareader to object list why not use sql to linq. Take a look at this stackoverflow.com/questions/1464883/… Commented Mar 7, 2015 at 13:23

1 Answer 1

2

What is in the CustomerTableColumns, could you show us the code?

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

Sign up to request clarification or add additional context in comments.

2 Comments

` public enum CustomerTableColumns { customerID = 0, firstName, surname, email, phone, addressLine1, addressLine2, city, county }`
exactly the problem. firstName = 1 and county =8, your datareader will have at index 0 the firstName.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.