ESQL throws Object Context error on server

49 views Asked by At

I am getting a The "ObjectContext instance has been disposed and can no longer be used for operations that require a connection error" for a code snippet - on the second query (the DataBind). It works fine in development. Development uses Entity Framework version 6 - the server may be version 4. here's the code:

    using (MainModelContainer m = new MainModelContainer())
    {
        AlltheNames = m.PlayerStatsNames.OrderBy(x => x.FieldName).ToArray();
        HeadingNames = new SortedDictionary<string, string>();
        for (i=0; i < AlltheNames.Length; i++)
        {
            HeadingNames.Add(AlltheNames[i].FieldName, AlltheNames[i].ShortHeader);
        }

        m.Connection.Open();
        var FieldNames = new ObjectQuery<DbDataRecord>(theQuery, m).ToArray();
        foreach(DbDataRecord FieldName in FieldNames)
        {
            if ((bool)FieldName[1])
            {
                sb.Append(",ps.");
                sb.Append(FieldName[0].ToString());
            }
        }
        sb.Append(" FROM PlayerStats AS ps ");
        sb.Append("LEFT OUTER JOIN RealTeams AS rt ON ps.RealTeamId=rt.RealTeamId WHERE ps.Player.PlayerId=");
        sb.Append(PlayerID);
        var PlayerStatistics = m.CreateQuery<DbDataRecord>(sb.ToString());
        rgStats.DataSource = PlayerStatistics;
        try
        {
            rgStats.DataBind();
        }
        catch (Exception ex)
        {

        }
    }
0

There are 0 answers