Professional Documents
Culture Documents
1of8
theirhiringanniversaryinthespecifiedMonth.Ourexamplewillsetthisparameterprogrammaticallybasedon thecurrentmonth,showingalistof"EmployeeAnniversariesThisMonth." Let'sgetstarted! Step1:AddingaMethodtoEmployeesTableAdapter ForourfirstexampleweneedtoaddameanstoretrievethoseemployeeswhoseHireDate occurredina specifiedmonth.Toprovidethisfunctionalityinaccordancewithourarchitectureweneedtofirstcreatea methodinEmployeesTableAdapter thatmapstotheproperSQLstatement.Toaccomplishthis,startby openingtheNorthwindTypedDataSet.RightclickontheEmployeesTableAdapter labelandchooseAdd Query.
Figure2:AddaNewQuerytotheEmployeesTableAdapter ChoosetoaddaSQLstatementthatreturnsrows.WhenyoureachtheSpecifyaSELECT Statementscreenthe default SELECT statementfortheEmployeesTableAdapter willalreadybeloaded.SimplyaddintheWHERE clause:WHEREDATEPART(m,HireDate)=@Month.DATEPARTisaTSQLfunctionthatreturnsa particulardateportionofadatetime typeinthiscasewe'reusingDATEPART toreturnthemonthofthe HireDate column.
2of8
Figure3:ReturnOnlyThoseRowsWheretheHireDate ColumnisLessThanorEqualtothe @HiredBeforeDate Parameter Finally,changetheFillBy andGetDataBy methodnamesto FillByHiredDateMonth and GetEmployeesByHiredDateMonth,respectively.
3of8
Aswithourothermethodsinthisclass,GetEmployeesByHiredDateMonth(month) simplycallsdowninto theDALandreturnstheresults. Step3:DisplayingEmployeesWhoseHiringAnniversaryIsThisMonth Ourfinalstepforthisexampleistodisplaythoseemployeeswhosehiringanniversaryisthismonth.Startby addingaGridViewtotheProgrammaticParams.aspx pageintheBasicReporting folderandaddanew ObjectDataSourceasitsdatasource.ConfiguretheObjectDataSourcetousetheEmployeesBLL classwiththe SelectMethod setto GetEmployeesByHiredDateMonth(month).
4of8
Figure6:UsetheEmployeesBLL Class
5of8
6of8
Figure9:ClickontheLightningBoltIconinthePropertiesWindowtoListaWebControl'sEvents BothapproachesaddaneweventhandlerfortheObjectDataSource'sSelecting eventtothepage's codebehindclass.Inthiseventhandlerwecanreadandwritetotheparametervaluesusing e.InputParameters[parameterName],whereparameterName isthevalueoftheName attributeinthe <asp:Parameter> tag(theInputParameters collectioncanalsobeindexedordinally,asin e.InputParameters[index]).Tosetthemonth parametertothecurrentmonth,addthefollowingtothe Selecting eventhandler:
protectedvoidObjectDataSource1_Selecting(objectsender, ObjectDataSourceSelectingEventArgse) { e.InputParameters["month"]=DateTime.Now.Month }
Whenvisitingthispagethroughabrowserwecanseethatonlyoneemployeewashiredthismonth(March) LauraCallahan,who'sbeenwiththecompanysince1994.
7of8
Figure10:ThoseEmployeesWhoseAnniversariesThisMonthAreShown Summary WhiletheObjectDataSource'sparameters'valuescantypicallybesetdeclaratively,withoutrequiringalineof code,it'seasytosettheparametervaluesprogrammatically.Allweneedtodoiscreateaneventhandlerfor theObjectDataSource'sSelecting event,whichfiresbeforetheunderlyingobject'smethodisinvoked,and manuallysetthevaluesforoneormoreparametersviatheInputParameters collection. ThistutorialconcludestheBasicReportingsection.Thenexttutorial kicksofftheFilteringandMasterDetails Scenariossection,inwhichwe'lllookattechniquesforallowingthevisitortofilterdataanddrilldownfroma masterreportintoadetailsreport. HappyProgramming! AbouttheAuthor ScottMitchell,authorofsixASP/ASP.NETbooksandfounderof4GuysFromRolla.com,hasbeenworking withMicrosoftWebtechnologiessince1998.Scottworksasanindependentconsultant,trainer,andwriter, recentlycompletinghislatestbook,SamsTeachYourselfASP.NET2.0in24Hours.Hecanbereachedat mitchell@4guysfromrolla.com orviahisblog,whichcanbefoundathttp://ScottOnWriting.NET. SpecialThanksTo Thistutorialserieswasreviewedbymanyhelpfulreviewers.LeadreviewerforthistutorialwasHilton Giesenow.InterestedinreviewingmyupcomingMSDNarticles?Ifso,dropmealineat mitchell@4GuysFromRolla.com.
8of8