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