2012-01-04 77 views
1

我想在存儲過程的幫助下在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 

該範圍之外的索引,儘管在數據庫中有多條記錄。 我該如何解決我的問題?

回答

1

這將是最好使用列別名和那些而是引用列。

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"]; 

如果你不能改變你的存儲過程,那麼你可以使用oridinal位置:

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

我必須讓我的存儲過程chenges,它的正常工作。但我不明白我的代碼有什麼問題?爲什麼它的劑量工作? – Michael 2012-01-04 17:26:33

+1

用你原來的代碼你將返回兩個同名的列。當SQL將結果返回給客戶端時,它只返回列的名稱,而不是表和列。如果你要遍歷所有的字段名稱,你會注意到兩個「名稱」實例。如果您要訪問reader [「Name」],它只會返回第一個匹配列的數據。 – 2012-01-04 17:47:52

+0

Thenk你尼克! – Michael 2012-01-04 19:01:27

1

我更正了你的錯誤。 (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(); 
     } 

    } 
相關問題