C# .NET - how to bind list<EntityClass> object

Asked By anbu n on 15-Mar-12 07:06 AM
how to bind list<EntityClass> object , using Sqlhelper, without using dataset or datatable  .from sql DB table.

i am using VS 2008 , with 3.5 .net framework


EntityClass :

public class EntityClass
    {
      public int Master_Static_Id { get; set; }
      public string Static_name { get; set; }

    }

Somesh Yadav replied to anbu n on 15-Mar-12 07:40 AM

If you already have an Entity type that matches your proc return type, use it as the type parameter.

public List<MyEntity> GetData<MyEntity>(int product_id) where T : class 
{

   
List<MyEntity> myList = new List<MyEntity>();

   
var groupData = context.ExecuteStoreQuery<MyEntity>("exec
    spGetProductsByGroup
@ProductID={0}", product_id);

   
return myList;
}

Otherwise you could use an http://msdn.microsoft.com/en-us/library/haa3afyz%28v=VS.100%29.aspx to build the list manually.

using (SqlConnection connection = new SqlConnection("your connection string"))
{
   
SqlCommand command = new SqlCommand(
     
"exec spGetProductsByGroup @ProductID",
      connection
);
    command
.Parameters.Add(product_id);

    connection
.Open();

   
SqlDataReader reader = command.ExecuteReader();

   
List<ProcType> list = new List<ProcType>();
   
if (reader.HasRows)
   
{
       
while (reader.Read())
       
{
            list
.Add(new ProcType(){Property1 = reader.GetInt32(0), Property1 = reader.GetString(1));
       
}
   
}
    reader
.Close();

   
return list;
}
kalpana aparnathi replied to anbu n on 15-Mar-12 07:45 AM
hi,

Try below code for binding list<entityclass>

if (!Page.IsPostBack)
   {
   using (MyBlogDataContextDataContext mC =
           new MyBlogDataContextDataContext())
   {
      List<UserInfo> MyList =
        mC.GetAllUsers().ToList<UserInfo>();
      grdUserList.DataSource = MyList;
      grdUserList.DataBind(); 
    }
   }

Regards,