4

Can someone help me understand this error? I'm getting it on the line new DbSet<Survey>(); in the block code

    public DbSet<Survey> GetAllSurveys ( )
    {
        DbSet<Survey> AllSurveys = new DbSet<Survey>();
        using ( SqlCommand cmd = new SqlCommand("GetAllSurveys", this._Conn) )
        {
           cmd.CommandType = CommandType.StoredProcedure;
           this._Conn.Open();

           using ( SqlDataReader dataReader = cmd.ExecuteReader() )
           {
                while ( dataReader.Read() )
                {
                    Survey srv = new Survey { Id = dataReader.IsDBNull(0) ? default(int) : dataReader.GetInt32(0),
                                              Title = dataReader.IsDBNull(1) ? String.Empty: dataReader.GetString(1) };
                    AllSurveys.Add(srv);
                }
           }
           this._Conn.Close();
        }
        return AllSurveys;
    }

What's strange is that I'm not getting it when I create a DbSet<T> right below:

    public DbSet<Question> GetQuestionsBySurveyId ( int survid )
    {
        SqlCommand cmd = new SqlCommand("GetQuestionsBySurveyId", this._Conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@id", survid);
        this._Conn.Open();
        DbSet<Question> TheseQuestions = (DbSet<Question>)cmd.ExecuteScalar();
        this._Conn.Close();
        return TheseQuestions;
    }

has no errors associated with it. What's the difference between these two?

user5648283
  • 5,913
  • 4
  • 22
  • 32
  • In what line exactly you're getting this error? Is it a compiler error? – abatishchev Jan 29 '16 at 05:50
  • 3
    `DbSet` is an abstract class, of course you can't create a new one directly, but can cast it. https://msdn.microsoft.com/en-us/library/system.data.entity.dbset%28v=vs.113%29.aspx – Eris Jan 29 '16 at 05:53
  • Maybe you are looking for DataTable? http://stackoverflow.com/questions/6073382/read-sql-table-into-c-sharp-datatable – Jannik Jan 29 '16 at 05:56

0 Answers0