I don't know the code you are talking about, and frankly going from
dataset --> custom collection seems a little on the stupid side. Its much
more logical to go from a datareader --> custom collection, which avoids the
entire dataset overhead.
From what you are saying, they are doing:
Dim ds as new dataset
dim da as new SqlDataAdapter(cmd)
da.fill(ds)
dim cc as new CustomCollection()
foreach row as DataRow in ds.tables[0].Rows
cc.add(new customItem(row))
next
in essense turning each row into a strongly-typed object and adding that to
a strongly-typed collection.
I'd change the code to be like:
SqlDataReader dr = cmd.executeReader();
dim cc as new CustomCollection()
while (dr.read())
cc.add(new customItem(dr))
end while
If you are asking about why pick a custom collection w/strongly typed object
in the first place vs a dataset, well, the answer is pretty simple:
- custom collections are more performant (datasets are bloated)
- far more importantly, custom collections return strongly-typed objects, vs
dataset which are all weakly typed, you tell me, which is less error prone:
dim customId as integer =
Convert.ToInt32(ds.tables("customers").Rows[0]("customerId"));
vs
dim customId as integer = customers(0).customerId;
The first code can throw 4 different exceptions:
- conversion could fail (maybe customId is null?),
- table named "customers" could be null,
- row 0 might be null
- field might be named something else or not exists
the 2nd code can throw 1 exception:
- customers(0) might be null. (ok, customers could be null too, but then
you have to say ds could be null in the above example).
you also get intellisense support with the latter and not the former which
is a big plus.
Now, all those things you are doing in the first case, you'll still need to
do when converting your IDataRecord or DataRow to your strongly-typed
object...but that'll be isolated into a single function, as opposed to you
having to do it (and thus changed it if something breaks) multiple places.
Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/
"rodchar" <ro*****@discussions.microsoft.com> wrote in message
news:28**********************************@microsof t.com...
hey all,
i was checking out time tracker on asp.net one of the starter kit
projects. i was wondering why the users datagrid gets loaded the way does. first it
pulls all the users in from a stored procedure into a dataset. next in the
code it iterates row by row instantiating a new user object and adding it
to a collection?
why would you do this when you can work directly with the dataset?
i'm very new to this just trying to understand concepts.
thanks in advance,
rodchar