none
Generics RRS feed

  • Question

  • protected void BindUserDetails()

    {
    objUserDetails = GetUserDetails();
    gvDetails.DataSource = objUserDetails;
    gvDetails.DataBind();
    }
    protected List<UserDetails> GetUserDetails()
    {
    string strConnection = "Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB";
    DataTable dt = new DataTable();
    SqlConnection con = new SqlConnection(strConnection);
    con.Open();
    SqlCommand cmd = new SqlCommand("select * from UserInformation", con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(dt);
    if(dt.Rows.Count>0)
    {
    for(int i=0;i<dt.Rows.Count;i++)
    {
    UserDetails userinfo = new UserDetails();
    userinfo.UserName = dt.Rows[i]["UserName"].ToString();
    userinfo.FirstName = dt.Rows[i]["FirstName"].ToString();
    userinfo.LastName = dt.Rows[i]["LastName"].ToString();
    userinfo.Location = dt.Rows[i]["Location"].ToString();
    objUserDetails.Add(userinfo);
    }
    }
    return objUserDetails;
    }

    Dear Sir I am implemented Generics in my project and bind the Gridview my question is in database for example i have 1000 records are there

    my for Loop while loop 1000 times and bind the Generic class i think we have already datatable is there just i can bind it is simple when compare to looping through datatable and prepare the generic class after binding.

    but microsot says generics are recommended i dont know how to use.Please clarify .............


    Thursday, November 15, 2012 9:37 AM

Answers

  • Data table occupies bit more memory space then a List of typed object.  That is the reason a list of DTO is suggested.  Your question is been eliminated by most ORM like EF/nHibernate.

    There is no direct way in plain ado.net to convert a datareader to UserDetails object. There is a open source initiative@ http://code.google.com/p/dapper-dot-net/ which would do that job for you.


    Lingaraj Mishra

    • Marked as answer by Alexander Sun Friday, December 7, 2012 2:46 AM
    Thursday, November 15, 2012 12:30 PM

All replies

  • Data table occupies bit more memory space then a List of typed object.  That is the reason a list of DTO is suggested.  Your question is been eliminated by most ORM like EF/nHibernate.

    There is no direct way in plain ado.net to convert a datareader to UserDetails object. There is a open source initiative@ http://code.google.com/p/dapper-dot-net/ which would do that job for you.


    Lingaraj Mishra

    • Marked as answer by Alexander Sun Friday, December 7, 2012 2:46 AM
    Thursday, November 15, 2012 12:30 PM
  • I'm not sure exactly what your question is, but I'd like to make a few comments:

    1. Rather than using List<UserDetails>, you should probably use BindingList<UserDetails>, since you're using it as a DataSource to your grid (although, it appears you're talking about an ASP.NET grid, and I don't know if BindingList applies to web stuff, as I deal only with desktop stuff).
    2. As an alternative, there's nothing wrong with binding your grid directly to the DataTable instead of the list.

    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

    • Proposed as answer by Alexander Sun Friday, December 7, 2012 2:46 AM
    Sunday, November 18, 2012 5:12 PM