Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I would be grateful if you could help me to solve the problem.
Im tring to fill my list on Visual Studio side with help of stored procedure.
I'm using the following stored procedure:

SQL
CREATE PROCEDURE [dbo].[GetColletcion]
AS
BEGIN   
    select  CollectionType.Name ,GlassesCollection.Name 
    from    GlassesCollection
    inner join CollectionType
    on GlassesCollection.CollectionType=CollectionType.CollTypeID
END


Here's the code-behind:

C#
protected void Button1_Click(object sender, EventArgs e)
        {
            List<GlassesCollection> list = new List<GlassesCollection>();
            using (SqlConnection conn = new SqlConnection("Server=(local);DataBase=ISeeOptic;Integrated Security=SSPI"))
            {
                GlassesCollection gln = new GlassesCollection();
                SqlCommand cmd = new SqlCommand();
                SqlDataReader reader;
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "GetColletcion";
                conn.Open();
                reader = cmd.ExecuteReader();
                  while (reader.Read())
                    {
                    gln.Name = (string)reader["CollectionType.Name"];
                    gln.CollectionType = (string)reader["GlassesCollection.Name"];
                    list.Add(gln);
                    }

                reader.Close();
                conn.Close();
            }
        }


But when it comes to this row:
C#
gln.Name = (string)reader["CollectionType.Name"];


I get this Exception:

Exception Details: System.IndexOutOfRangeException: CollectionType.Name
The index outside of the range, although in database more than one record. How can i solve my problem.

Please help me.

Regards
Posted

1 solution

first check if your SP (Stored procedure) is returning any results.
Secondly, try to change the line:
C#
gln.Name = (string)reader["CollectionType.Name"];

for
C#
gln.Name = (string)reader["Name"];


And if the previus solutions don't work, obviously the name of the returned data is givin you an error, so try to use a Alias for each column of your SP, for example:
SQL
CREATE PROCEDURE [dbo].[GetColletcion]
AS
BEGIN
select  CollectionType.Name ,GlassesCollection.Name
from    GlassesCollection
inner join CollectionType
on GlassesCollection.CollectionType=CollectionType.CollTypeID
END


change to
SQL
CREATE PROCEDURE [dbo].[GetColletcion]
AS
BEGIN
select  CollectionType.Name AS CollectionName ,GlassesCollection.Name AS GlassesName
from    GlassesCollection
inner join CollectionType
on GlassesCollection.CollectionType=CollectionType.CollTypeID
END


and modify your mapping like this:
C#
gln.Name = (string)reader["GlassesName"];


Cheers :D
 
Share this answer
 
v2
Comments
bbirajdar 4-Jan-12 10:27am    
excellent one.. I was thinking the same and found this posted already.. 4* for you( without testing)
darkDercane 4-Jan-12 10:30am    
thank u!! i'm glad that the solution helps :)
Mich_90 4-Jan-12 12:38pm    
I have make chenges on my stored procedure and it's work properly.But i cant understand what the problem with my code?why it dosent work?
darkDercane 4-Jan-12 12:40pm    
the mapping name is not correct, you must distinguish between the different columns in your reader, the name on your mapping is wrong, try to change the "CollectionType.Name" for anything else :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900