to sqlDataSource or not - scenario

D

djc

1) I am wondering if I should be using an sqlDataSource object for my
particular scenario. I need to loop through a listbox and perform an INSERT
sql operation for each item. Could be a few or several items. The reason I'm
wondering if I should use an SqlDataSource object is overhead. For example,
prior to learning about the sqlDataSource I would just code the ado.net
procedure myself. For example, create connection object, create and
configure SqlCommand with parameters, open the connection once, perform
*all* the INSERT operations, then close the connection. If I use an
SqlDataSource object explicitly calling it's Insert command each time
through the listbox loop will it open and close the connection to the
database each time? or even worse, create a connection object, open it, and
close it, for each and every INSERT? If so I assume that would be very
undesireable.

2) Even if I don't use the sqlDataSource right now I need clarification on
this related topic. Someone else already asked this better than me so I'm
just copy/pasting this:
If I have a SqlDataSource with a @LastName parameter and want to assign a
value at runtime how do I do it without using Controls, Session Variables,
etc.

For instance what I expect is:

SqlDataSource.SelectQuery = "SELECT * FROM Employees WHERE LastName =
@LastName"
SqlDataSource.Parameter(@LastName) = "Smith"

The 'accepted answer' to this question from Experts Exchange was to use the
DefaultValue property like so:
SqlDataSource.SelectParameters("LastName").DefaultValue = "Smith"

This doesn't seem right to me? (or the original poster on EE). I dont want
to assign a 'default' value, I want to assign 'the' value. It does work but
should it be used?

The other way suggested to me was to use events as the links below show.

these 2 links provided to me by Andrey K illustrate using the OnInsert event
to change parameter values, and validate them
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.insertcommand.aspx
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.inserting.aspx

which way to go?

any input on these 2 questions would be greatly appreciated. Thanks.
 
G

Guest

This is just "opinion" so take it with a grain of salt. I like the
SqlDataSource and especially the ObjectDataSource controls. However, I
believe they were provided to "dummify" - enabling developers to make things
happen without (in many cases) having to write any code. If you feel
comfortable rolling your own, its a good bet you can do what you need to do
more efficiently, without having to go through the learning curve.

Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com
 
D

djc

Thanks for the input Peter. I am comfortable rolling my own in this
particular scenario, but not always. Do you know if using the SqlDataSource
for this scenario *would* behave as I suspect? (several iterations of
opening and closing the database unnecessarily)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top