LimitationsofAutomaticallyGeneratingSQLStatements
Sincethe“Generate
INSERT
,
UPDATE
,and
DELETE
statements” optionisonlyavailablewhenpickingcolumnsfrom
atable,formorecomplexqueriesyouwillhavetowriteyourown
INSERT
,
UPDATE
,and
DELETE
statementslikewe
didinStep1.Commonly,SQL
SELECT
statementsuse
JOIN
stobringbackdatafromoneormorelookuptablesfor
displaypurposes(suchasbringingbackthe
Categories
table’s
CategoryName
fieldwhendisplayingproduct
information).Atthesametime,wemightwanttoallowtheusertoedit,update,orinsertdataintothe“core”table
(
Products
,inthiscase).
Whilethe
INSERT
,
UPDATE
,and
DELETE
statementscanbeenteredmanually,considerthefollowingtimesaving
tip.InitiallysetuptheSqlDataSourcesothatitpullsbackdatajustfromthe
Products
table.UsetheConfigure
DataSourcewizard’s“Specifycolumnsfromatableorview”screensothatyoucanautomaticallygeneratethe
INSERT
,
UPDATE
,and
DELETE
statements.Then,aftercompletingthewizard,choosetoconfiguretheSelectQuery
fromthePropertieswindow(or,alternatively,gobacktotheConfigureDataSourcewizard,butusethe “Specifya
customSQLstatementorstoredprocedure” option).Thenupdatethe
SELECT
statementtoincludethe
JOIN
syntax.
ThistechniqueoffersthetimesavingbenefitsoftheautomaticallygeneratedSQLstatementsandallowsfora
morecustomized
SELECT
statement.
Anotherlimitationofautomaticallygeneratingthe
INSERT
,
UPDATE
,and
DELETE
statementsisthatthecolumnsin
the
INSERT
and
UPDATE
statementsarebasedonthecolumnsreturnedbythe
SELECT
statement.Wemayneedto
updateorinsertmoreorfewerfields,however.Forexample,intheexamplefromStep2,maybewewanttohave
the
UnitPrice
BoundFieldbereadonly.Inthatcase,itshouldn’ tappearinthe
UpdateCommand
.Orwemaywant
tosetthevalueofatablefieldthatdoesnotappearintheGridView.Forexample,whenaddinganewrecordwe
maywantthe
QuantityPerUnit
valuesetto“TODO”.
Ifsuchcustomizationsarerequired,youneedtomakethemmanually,eitherthroughthePropertieswindow,the
“SpecifyacustomSQLstatementorstoredprocedure” optioninthewizard,orviathedeclarativesyntax.
Note:
WhenaddingparametersthatdonothavecorrespondingfieldsinthedataWebcontrol,keepinmindthat
theseparametersvalueswillneedtobeassignedvaluesinsomemanner.Thesevaluescanbe:hardcodeddirectly
inthe
InsertCommand
or
UpdateCommand
;cancomefromsomepredefinedsource(thequerystring,sessionstate,
Webcontrolsonthepage,andsoon);orcanbeassignedprogrammatically,aswesawintheprecedingtutorial.
Summary
InorderforthedataWebcontrolstoutilizetheirbuiltininserting,editing,anddeletingcapabilities,thedata
sourcecontroltheyareboundtomustoffersuchfunctionality.FortheSqlDataSource,thismeansthat
INSERT
,
UPDATE
,and
DELETE
SQLstatementsmustbeassignedtothe
InsertCommand
,
UpdateCommand
,and
DeleteCommand
properties.Theseproperties,andthecorrespondingparameterscollections,canbeaddedmanually
orgeneratedautomaticallythroughtheConfigureDataSourcewizard.Inthistutorialweexaminedboth
techniques.
WeexaminedusingoptimisticconcurrencywiththeObjectDataSourceintheImplementingOptimistic
Concurrency tutorial.TheSqlDataSourcecontrolalsoprovidesoptimisticconcurrencysupport.AsnotedinStep2,
whenautomaticallygeneratingthe
INSERT
,
UPDATE
,and
DELETE
statements,thewizardoffersa“Useoptimistic
concurrency” option.Aswe’llseeinthenexttutorial,usingoptimisticconcurrencywiththeSqlDataSource
modifiesthe
WHERE
clausesinthe
UPDATE
and
DELETE
statementstoensurethatthevaluesfortheothercolumns
haven’tchangedsincethedatawaslastdisplayedonthepage.
HappyProgramming!
11 of12