索引超出范围Exception [英] The index outside of the range Exception

查看:70
本文介绍了索引超出范围Exception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果能帮助我解决问题,我将不胜感激.
我希望借助存储过程来在Visual Studio方面填充我的列表.
我正在使用以下存储过程:

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:

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:

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:

gln.Name = (string)reader["CollectionType.Name"];



我收到此异常:

异常详细信息:System.IndexOutOfRangeException: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

推荐答案

首先检查您的SP(存储过程)是否返回任何结果.
其次,尝试更改该行:
first check if your SP (Stored procedure) is returning any results.
Secondly, try to change the line:
gln.Name = (string)reader["CollectionType.Name"];


对于


for

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



而且,如果以前的解决方案不起作用,显然返回的数据名称会给您一个错误,因此请尝试对SP的每一列使用别名,例如:



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:

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



更改为



change to

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:

gln.Name = (string)reader["GlassesName"];



欢呼声:D



Cheers :D


这篇关于索引超出范围Exception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆