question

technette avatar image
technette asked

Index out of Range Exception

I have a stored procedure that is returning multiple result sets. In my data service, I am trying to read each result set separately and to display in datagrids correctly. I am getting an 'Index out of Range Exception' on 'LanguageDescription'. Don't understand why. While reader.Read() Dim d As DateTime If reader("LastUsed") Is DBNull.Value Then d = DateTime.MinValue Else d = Convert.ToDateTime(reader("LastUsed")) End If list.Add(New EmpHistDetails_vw() With { _ .LanguageCodeDescription = reader("LanguageCodeDecription").ToString(), _ .LastUsed = d _ }) reader.NextResult() list.Add(New EmpHistDetails_vw() With { _ .CourseDescription = reader("CourseDescription").ToString(), _ .EducationLevelDescription = reader("EducationLevelDescription").ToString(), _ .Emp = reader("Emp").ToString(), _ .EmpName = reader("EmpName").ToString(), _ .InstituteDescription = reader("InstituteDescription").ToString() _ }) reader.NextResult() list.Add(New EmpHistDetails_vw() With { _ .RegDescription = reader("RegDescription").ToString(), _ .RegStateDescription = reader("RegStateDescription").ToString() _ }) reader.NextResult() list.Add(New EmpHistDetails_vw() With { _ .SF254Desc = reader("SF254Desc").ToString(), _ .SkillDescription = reader("SkillDescription").ToString(), _ .PrimaryVsAlternate = reader("PrimaryVsAlternate").ToString() _ }) End While
sql-server-2005entityframework
1 comment
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

I Believe its not related to SQL Server
0 Likes 0 ·
Matt Whitfield avatar image
Matt Whitfield answered
It's because you're using SqlDataReader totally wrong :) NextResult moves from one result set to the next, and Read() reads rows from the current result set. So what you are doing, is reading 1 row from each result set, then looping back round (because result set 4 has more than one row) and treating result set 4 as if it had the schema of result set 1. You really want something that is structured more like this: while reader.Read() { // do stuff with rows from first result set } if (reader.NextResult()) { while reader.Read() { // do stuff with rows from second result set } } if (reader.NextResult()) { while reader.Read() { // do stuff with rows from third result set } } if (reader.NextResult()) { while reader.Read() { // do stuff with rows from fourth result set } }
2 comments
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Thank you for responding. I changed the code also adding end whiles and end ifs where applicable and it worked.
0 Likes 0 ·
No worries, glad I could help. Nice to see someone has found the green tick! :)
0 Likes 0 ·
ThomasRushton avatar image
ThomasRushton answered
The likelihood is that you've made a typographical error - and that the field isn't "LanguageDescription". Indeed, looking at the code sample you provide, I can't see "LanguageDescription" anywhere. What you have got is "**LanguageCodeDescription**".
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.