Out of range error on SqlDataReader

前端 未结 2 928
礼貌的吻别
礼貌的吻别 2021-01-20 18:19

I\'m using a SqlDataReader and get this exception when trying to read a column...

System.IndexOutOfRangeException: record

相关标签:
2条回答
  • In my case this error was because I was accidentally executing two select statements in my command text. I had two separate output results and the first output didn't contain the column name I was reading and hence the error.

    0 讨论(0)
  • 2021-01-20 18:57

    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 />";
    
    0 讨论(0)
提交回复
热议问题