Passing URL Parameters to Stored Procedures

Labels
AJAX(112) App Studio(8) Apple(1) Application Builder(245) Application Factory(207) ASP.NET(95) ASP.NET 3.5(45) ASP.NET Code Generator(72) ASP.NET Membership(28) Azure(18) Barcode(2) Barcodes(3) BLOB(18) Business Rules(1) Business Rules/Logic(140) BYOD(13) Caching(2) Calendar(5) Charts(29) Cloud(14) Cloud On Time(2) Cloud On Time for Windows 7(2) Code Generator(54) Collaboration(11) command line(1) Conflict Detection(1) Content Management System(12) COT Tools for Excel(26) CRUD(1) Custom Actions(1) Data Aquarium Framework(122) Data Sheet(9) Data Sources(22) Database Lookups(50) Deployment(22) Designer(177) Device(1) DotNetNuke(12) EASE(20) Email(6) Features(101) Firebird(1) Form Builder(14) Globalization and Localization(6) How To(1) Hypermedia(2) Inline Editing(1) Installation(5) JavaScript(20) Kiosk(1) Low Code(3) Mac(1) Many-To-Many(4) Maps(6) Master/Detail(36) Microservices(4) Mobile(63) Mode Builder(3) Model Builder(3) MySQL(10) Native Apps(5) News(18) OAuth(9) OAuth Scopes(1) OAuth2(13) Offline(20) Offline Apps(4) Offline Sync(5) Oracle(11) PKCE(2) Postgre SQL(1) PostgreSQL(2) PWA(2) QR codes(2) Rapid Application Development(5) Reading Pane(2) Release Notes(184) Reports(48) REST(29) RESTful(29) RESTful Workshop(15) RFID tags(1) SaaS(7) Security(81) SharePoint(12) SPA(6) SQL Anywhere(3) SQL Server(26) SSO(1) Stored Procedure(4) Teamwork(15) Tips and Tricks(87) Tools for Excel(2) Touch UI(93) Transactions(5) Tutorials(183) Universal Windows Platform(3) User Interface(338) Video Tutorial(37) Web 2.0(100) Web App Generator(101) Web Application Generator(607) Web Form Builder(40) Web.Config(9) Workflow(28)
Archive
Blog
Friday, August 8, 2014PrintSubscribe
Passing URL Parameters to Stored Procedures

Code On Time generator allows creating data controllers from the result set of a stored procedure. Some stored procedures use parameters in order to perform operations on the data. For example, the EmployeeSalesByCountry stored procedure in the sample Northwind database takes two parameters (@Beginning_Date and @Ending_Date) in order to filter the returned result by the two dates.

Let’s allow the controller to accept URL parameters to set the @Beginning_Date and @Ending_Date for the stored procedure, as shown in the picture.

Controller generated from stored procedure accepts URL arguments as parameters.

The stored procedure definition can be seen below.

CREATE procedure dbo.[Employee Sales by Country] 
@Beginning_Date DateTime, @Ending_Date DateTime AS
SELECT    Employees.Country, 
        Employees.LastName, 
        Employees.FirstName, 
        Orders.ShippedDate, 
        Orders.OrderID, 
        "Order Subtotals".Subtotal AS SaleAmount
FROM Employees INNER JOIN 
    (Orders INNER JOIN "Order Subtotals" ON Orders.OrderID = "Order Subtotals".OrderID) 
    ON Employees.EmployeeID = Orders.EmployeeID
WHERE Orders.ShippedDate Between @Beginning_Date And @Ending_Date

Let’s create the data controller with an SQL query that will take the parameters from the URL if present. Otherwise, it will use default values. Our script will specify defaults that allow all records to be seen. If necessary, the default values could be set to the same date in order to return zero records when URL parameters are not specified.

Start the Project Designer. In the Project Explorer, switch to the Controllers tab. Click on the New Controller icon on the toolbar.

Adding a new controller.

Enter a name for the controller.

Property Value
Name EmployeeSalesByCountry

Press OK to save. Right-click the new controller and press “Generate From SQL…”.

Generating the controller from SQL query.

In the SQL script textbox, paste in the following script. The debug section is removed from the business rule when the application framework executes the script at runtime and declares the URL parameters.

-- debug
declare @Url_BeginningDate datetime,
        @Url_EndingDate datetime
-- end debug

declare @Beginning_Date datetime,
        @Ending_Date datetime

if (@Url_BeginningDate is not null)
    set @Beginning_Date = @Url_BeginningDate
else 
    set @Beginning_Date = '1970-01-01'

if (@Url_EndingDate is not null)
    set @Ending_Date = @Url_EndingDate
else 
    set @Ending_Date = '2000-01-01'

EXEC dbo.[Employee Sales by Country] @Beginning_Date, @Ending_Date

Press OK to generate the controller.

Next, let’s add the controller to a page. Right-click on the controller and press Copy.

Copying the EmployeeSalesByCountry controller.

Switch to the Pages tab in the Project Explorer. On the toolbar, press the New Page icon.

Adding a new page to the project.

Give a name to the page and press OK to save.

Property Value
Name EmployeeSalesByCountry

Drop the new page to the right side of Home page node to place it second in the site menu.

Dropping the page to the right side of Home page node.     The "Employee Sales By Country" page has been placed second in the site menu.

Right-click on the page and press Paste to instantiate the controller as a data view on the page.

Pasting onto the Employee Sales By Country page.     The EmployeeSalesByCountry controller has been instantiated as a data view on the page.

On the toolbar, press Browse. The page will open in your default browser. Note that all 809 items are displayed.

All 809 items are displayed on the page before the URL parameter has been specified.

In the URL bar of the browser, replace everything after “.aspx” with the following:

?BeginningDate=1996-07-01&EndingDate=1996-08-01

Press Enter. Notice that only 17 items are displayed.

The URL arguments are passed as parameters for the stored procedure.

The next steps may be to create a Navigate action that redirects to this page and sets the URL parameters.