2012-01-04 20 views
1

私はストアドプロシージャの助けを借りてVisual Studio側で私のリストを記入しようとしています。範囲外のインデックス例外

私は次のストアドプロシージャを使用しています:

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

     } 

しかし、それは、この行に来る:

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

は、ここでは、コードビハインドだ

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

+1

元のコードでは、同じ名前の2つの列を返していました。 SQLは結果をクライアントに返すときには、テーブルとカラムではなく、カラムの名前だけを返します。すべてのフィールド名をループする場合は、「名前」という2つのインスタンスがあります。読者["Name"]にアクセスした場合、一致する最初の列のデータのみが返されます。 –

+0

あなたはニック! – Michael

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

    } 
関連する問題