索引超出范围异常

发布于 2024-12-25 05:42:08 字数 1678 浏览 1 评论 0原文

我正在尝试在存储过程的帮助下在 Visual Studio 端填充我的列表。

我正在使用以下存储过程:

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

这是隐藏的代码:

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();
            }

        }

但是当涉及到这一行时:

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

我得到这个异常:

Exception Details: System.IndexOutOfRangeException: CollectionType.Name

索引超出范围,尽管在数据库中有多个记录。 我该如何解决我的问题?

I'm trying 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"];

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

入怼 2025-01-01 05:42:08

最好使用列别名并通过这些别名来引用列。

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

然后使用

(string)reader["TypeName"];
(string)reader["GlassesName"];

如果您无法更改存储过程,那么您可以使用原始位置:

(string)reader[0]; //CollectionType.Name
(string)reader[1]; //GlassesCollection.Name

It would be best to use column aliases and refer to the columns by those instead.

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

Then use

(string)reader["TypeName"];
(string)reader["GlassesName"];

If you cannot change your stored procedure, then you can use the oridinal position:

(string)reader[0]; //CollectionType.Name
(string)reader[1]; //GlassesCollection.Name
平生欢 2025-01-01 05:42:08

我也纠正了你的错字。 (GetCollection)

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

背后的代码:

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 = "GetCollection";

            conn.Open();
            reader = cmd.ExecuteReader();
              while (reader.Read())
                {
                    gln.Name = (string)reader["GlassesCollection_Name"];
                    gln.CollectionType = (string)reader["CollectionType_Name"];

                    list.Add(gln);
                }


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

    }

I corrected your typo, too. (GetCollection)

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

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 = "GetCollection";

            conn.Open();
            reader = cmd.ExecuteReader();
              while (reader.Read())
                {
                    gln.Name = (string)reader["GlassesCollection_Name"];
                    gln.CollectionType = (string)reader["CollectionType_Name"];

                    list.Add(gln);
                }


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

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文