Calling Stored Procedures from Canvas Apps

Canvas Apps

Canvas Apps, a part of Microsoft’s Power Apps suite, is a powerful tool that allows users to create custom applications without needing to write extensive code. One of the many features it offers is the ability to call stored procedures from a database. This article will guide you through the process of calling a stored procedure from a Canvas App.

Prerequisites

Before we begin, ensure that you have the following.

  1. A Canvas App created in Power Apps.
  2. A database with a stored procedure that you want to call.
  3. The SQL Server connector is set up in your Canvas App.

Upcoming SQL SP

Steps to Call a Stored Procedure


Step 1. Add a Data Source

First, you need to add a data source to your Canvas App that connects to your database. To do this:

  1. Go to the View tab and click on Data sources.
  2. Click on Add data source and select the SQL Server connector.
  3. Enter the necessary details to connect to your database and click Connect.

Step 2. Call the Stored Procedure

Once the data source is set up, you can call the stored procedure. To do this.

Go to the screen where you want to call the stored procedure.

Add a button and set its OnSelect property to the following formula.

ClearCollect(
    ResultCollection,
    '[dbo].[YourStoredProcedureName]'(
        Param1: TextInput1.Text,
        Param2: TextInput2.Text
    )
)

Replace 'YourStoredProcedureName' with the name of your stored procedure and Param1, Param2 with the parameters your stored procedure requires. TextInput1.Text, TextInput2.Text are the inputs for these parameters.

Step 3. Use the Results

The ClearCollect function collects the results of the stored procedure into a collection named ResultCollection. You can use this collection in your app like this:

First(ResultCollection).ColumnName

Replace ColumnName with the name of the column you want to display.

Calling stored procedures from canvas apps can offer several advantages.

  1. Improved Performance

    • Stored procedures are compiled once and stored in executable form. When you call a stored procedure, it executes quickly and efficiently.
    • By offloading complex calculations or queries to the server, you keep your app lightweight and responsive.
  2. Centralized Logic

    • Stored procedures encapsulate business logic within the database.
    • You can maintain consistency by having a single point of truth for data manipulation.
  3. Security and Access Control

    • Stored procedures allow you to control access to specific database operations.
    • You can grant permissions to execute stored procedures while restricting direct table access.
  4. Parameter Passing

    • You can pass parameters to stored procedures, making them flexible and reusable.
    • This parameterization enables dynamic queries and customizations.
  5. Reduced Network Traffic

    • By executing stored procedures on the server, you minimize data transfer between the app and the database.
    • This optimization is especially beneficial for mobile apps with limited bandwidth.
  6. Code Reusability

    • You can save and reuse SQL code across different parts of your app.
    • This promotes consistency and reduces redundancy.