views:

47

answers:

3

Hi , I am writting a website using .net in some of my pages I need to use datagrid for showing some datas and for binding my data to datagrid I use object datasource something like below :

//this is 
public class userData
{
    private long userid = 0;
    private string username = null;
    private long amounts = 0;
    private DateTime registerdate;

    public long Userid
    {
        get { return userid; }
        set { userid = value; }
    }

    public string Username
    {
        get { return username; }
        set { username = value; }
    }

    public long Amounts
    {
        get { return amounts; }
        set { amounts = value; }

    }

    public DateTime Registerdate
    {
        get { return registerdate; }
        set
        { registerdate = value;}

    }

    }

//my businesshelper class

public class BusinessHelper
{
    public BusinessHelper()
    {
        //
        // TODO: Add constructor logic here
        //
    }


    public ICollection GetusersAmount()
    {
        ArrayList list = new ArrayList();
        DataSet1TableAdapters.userTableAdapter usert = new DataSet1TableAdapters.userTableAdapter();
        DataSet1.userDataTable ud = usert.GetData();


        foreach(DataSet1.userRow R in ud)
        {
            userData user = new userData();
            user.Amounts = R.Amount;
            user.Registerdate = R.registerdate;
            user.Userid = R.userID;
            user.Username = R.username ;
            list.Add(user);

        }
        return list;
    }
}

and then in my apsx file I put an objectdatasource and bind the select statement to the GetUsersAmount() and then bind obejctdatasource to my datagrid

but I think the way I've chosen is not efficient I just want to hear about your way of doing such these things.

regards.

A: 

I wouldn't use a dataset to populate your list of business objects, but would use a SqlDataReader or something along those lines. Datasets have much more overhead than DataReaders.

I also would use a Collection (or something based on IEnumerable) to return the list of business objects back. There is probably a cost involved in casting to and from an ArrayList.

You may also want to implement some paging support into your method, unless your datagrid displays every record at once.

Dan Scott
+2  A: 

You can start by using auto properties:

public class userData
{
  //  private long userid = 0;

    public long Userid    { get; set;   }

    ....
}

And, assuming you want to keep the TableAdapter:

// untested
DataSet1.userDataTable ud = usert.GetData();

var list = from r in ud.Rows.AsEnumerable()
           select new userData() { Userid = r.userID, Username = r.username }

return list; // maybe list.ToList()

Aa an aside, I would suggest taking a good look at your naming conventions, those lower case class names confused me quite a bit.

Henk Holterman
I don't know what "austin powers" meant by *efficient*. So just want to nitpick that above code is efficient from coding efforts perspective (and not otherwise).
VinayC
actually I want to fill up the datagrid in a custom way and manipulate some datas like converting Georgian calendar date to solar calendar date and so on ... and I thought to use ODS(Obejct Data Source) but I really don't know my way is correct or not? in aspect of paging and editing can I rely on ODS when its bind to datagrid?
austin powers
@VinaYc, the OP wants to feed it to a Datagrid. Any optimization here is going to be so tiny in comparison. I take efficiency as "easy and clear to write/maintain"
Henk Holterman
+1  A: 

I don't think that you are doing anything wrong (at the most, as suggested by other poster, you can use DataReader instead of data set). The class userData is typically called DTO (data transfer object) or entity class. You have other choices of DTO as well as - for example, you can use typed data tables or generic data sets. Custom classes are the most flexible choice but of course needs most efforts - because you have to hand-code them and write logic to fill/save them. There are frameworks (OR Mapper) to reduce these mundane efforts and generate data access code automatically. Example of such tools would be Entity Framework (ADO.NET Entity Model), Linq To Sql, NHibernate etc.

VinayC