I'm using a SqlDataReader and get this exception when trying to read a column...
System.IndexOutOfRangeException: record
Here is the code...
SqlCommand select = new SqlCommand("SELECT RTRIM(LTRIM(PART_NO)) AS PART_NO, record AS CUSTOMER_NO FROM [RMAData].[dbo].[IMPORTING_ORDER_EDI] WHERE sessionID = '" + Session.SessionID + "'", connection);
SqlDataReader reader = select.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
lblWebMasterMessage.Text += "record " + reader["record"].ToString() + "<br />";
...
If I change the lblWebMasterMessage.Text to the following it works just fine...
lblWebMasterMessage.Text += "record " + reader["PART_NO"].ToString() + "<br />";
The difference between record and PART_NO in the SQL Server table is that 'record' is a primary key and an int, PART_NO is a varchar(100).
The trouble is, I need the 'record' to identify the record to update it later...
I really can't see why it can return one field and not the other?
That's because you have no field named "record", you aliased it to "CUSTOMER_NO" so change the code to:
lblWebMasterMessage.Text += "record " + reader["CUSTOMER_NO"].ToString() + "<br />";
That said, you can also use index instead of name so to read the second column:
lblWebMasterMessage.Text += "record " + reader[1] + "<br />";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With