Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attempt to read from column ordinal '0' is not valid

Im having a problem with my first column ForumGroup in my stored procedure in my Entities collection. I get this error.

Attempt to read from column ordinal '0' is not valid. With CommandBehavior.SequentialAccess, you may only read from column ordinal '3' or greater.

My Sql:

    DECLARE @Forums TABLE(ForumGroup nvarchar(100), Title varchar(100), Description varchar(100), ThreadCount int, LastPostBy nvarchar(50),
LastPostDate datetime, LastPostTitle varchar(100))
INSERT INTO @Forums(ForumGroup, Title, Description, ThreadCount, LastPostBy, LastPostDate, LastPostTitle)
SELECT
(
    CASE WHEN F.ParentID IS NOT NULL THEN
        (SELECT Title FROM Forums S WHERE S.ForumID = F.ParentID)           
    ELSE
        (SELECT Title FROM Forums S WHERE S.ParentID IS NULL)
    END) AS ForumGroup,
Title, Description, 
ThreadCount = (SELECT COUNT(*) FROM Posts P WHERE  P.ForumID = F.ForumID),
LastPostBy = (SELECT TOP 1 AddedBy FROM Posts P WHERE P.ForumID = F.ForumID ORDER BY P.PostID DESC), 
LastPostDate = (SELECT TOP 1 AddedDate FROM Posts P WHERE P.ForumID = F.ForumID ORDER BY P.PostID DESC),
LastPostTitle = (SELECT TOP 1 Title FROM Posts P WHERE P.ForumID = F.ForumID ORDER BY P.PostID DESC) 

FROM Forums F WHERE ParentID IS NOT NULL

ORDER BY ForumGroup

SELECT * FROM @Forums

my C#:

public class Forums
{        
    public List<Forum> GetForums()
    {

        using (EntityConnection conn = new EntityConnection("name=CMSEntities"))
        {
            conn.Open();
            EntityCommand cmd = conn.CreateCommand();
            cmd.CommandText = "CMSEntities.sproc_Forums_GetForums";
            cmd.CommandType = System.Data.CommandType.StoredProcedure;

            using (EntityDataReader reader = cmd.ExecuteReader(System.Data.CommandBehavior.SequentialAccess))
            {
                List<Forum> forums = new List<Forum>();
                while (reader.Read())
                {
                    Forum forum = new Forum(
                        1,
                        "",
                        DateTime.Now,
                        reader["Title"].ToString(),
                        reader["Description"].ToString(),
                        0,
                        false,
                        null,
                        null,
                        null,
                        true,
                        reader["ForumGroup"].ToString(),
                        (int)reader["ThreadCount"],
                        null,
                        DateTime.Now,
                        null);
                    forums.Add(forum);
                }
                return forums;
            }
        }
    }
}
like image 509
ONYX Avatar asked Nov 23 '25 23:11

ONYX


1 Answers

Take a look at this blog post..

Quote to blog post:

If you open a reader with SequentialAccess you must read the columns sequentially, you can't read column 1 and then read column 0. You have to do it the other way around.

like image 190
KV Prajapati Avatar answered Nov 25 '25 11:11

KV Prajapati



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!