
Introduction
In this article, we will see in detail how to create a dynamic MVC dashboard display using AngularJS and Web API. Using this web application, you can write your own SQL query to bind dynamic dashboard. This program makes your work easy to display any Table/ Columns details with your entered where condition, Order BY and with Group By option for the selected database on your home page.
Features in Shanu MVC Dashboard

- Dynamic SQL Query:
- Column Names:
- Table Names:
- Where Condition:
- Group By:
- Order By:
Here, we will see details of each part,
1. Dynamic SQL Query
We can bind any data on our Dashboard page by entering our Select Query in this text box. We can also write our join query to display data from more than one table with where conditions. We need to first check the Is Query Checkbox for displaying our Dynamic SQL Query data on the dashboard page. If the check box is not checked then by default we will display the column and table details which we have given.

Note In this article, we can find the sample database and table creation query in code part. Before running this application kindly run the code part SQL script to create sample database and table with insert record for testing the output in our MVC dashboard page.
Sample Select query to be used for our application:
- select ID,ItemNo ,ItemName ,Comments ,Price from ItemDetail where Price>'1000'
- select A.UserName,A.UserType,A.Phone,B.Address,B.Email
- From
- Userdetails A Left Outer JOIN UserAddress B
- on
- A.UserID=B.UserID
Note: Now we have fixed for only one database, we can select any table dada from the given Database name in our web config.
2. Column Names & 3) Table NameNext we have another option to display the data by entering specific column or all columns to be displayed from the user entered tables .here user no need to write the complete Select SQL query. Here we can first write all our column names with comma and then write our table name for the given column details. We can also write our join query here by giving all the join column name details in the Column Names and join table name details with on Condition on the Table Name input. Here we can see a sample output for column and table details.

Sample Column Name and table details to be used for testing:
- Column Names: UserName, UserType, Phone
Table Names: UserDetails - Column Names: A.UserName, A.UserType, A.Phone, B.Address, B.Email
Table Names: Userdetails A Left Outer JOIN UserAddress B on A.UserID=B.UserID
Now let’s see how to write our where condition for the given column and table details. If user need to add some condition using then they can add there condition same like we write in our SQL query to add more conditions we can use and key word. Here in the following image we can see the output of using where condition in our dashboard page.
- Column Names: ItemName, Price
Table Names: ItemDetail
Where Condition: Price>'4000' - Column Names: ItemNo, ItemName, Comments, Price
Table Names:ItemDetail
Where Condition: ItemName like 'm%'
5. Group By
To use the Group By for our given column and table details user needs to check the Group By checkbox. In the Group By text box user can enter the column details for Group By. Here, we can see a sample output using Group By for the given column and Table details. Here, we can see as we have used both where condition and also unchecked the where condition to display the dynamic data with condition using Group By keyword.

Sample Column Name and table details with where condition and Group By to be used for testing:
- Column Names: ItemName,Price
Table Names: ItemDetail
Where Condition: Price>'4000'
Group By: ItemName
To use the Order By for our given column and table details user need to check the Order By checkbox. In the Order By text box user can enter the column details for displaying the data in any order as ascending or in descending. Here, we can see a sample output using Order By for the given column and table details.

Sample Column Name and table details with Order By:
- Column Names: ID, ItemNo, ItemName, Comments, Price
Table Names: ItemDetail
Order By: Price desc
SQL Injection checking in user entered select query

- // Array value to check for SQL Injection
- $scope.sqlInjectionArray = ['create', 'drop', 'delete', 'insert', 'update', 'truncate',
- 'grant', 'print', 'sp_executesql', 'objects', 'declare',
- 'table', 'into', 'sqlcancel', 'sqlsetprop', 'sqlexec',
- 'sqlcommit', 'revoke', 'rollback', 'sqlrollback', 'values',
- 'sqldisconnect', 'sqlconnect', 'system_user', 'schema_name',
- 'schemata', 'information_schema', 'dbo', 'guest', 'db_owner',
- 'db_', 'table', '@@', 'Users', 'execute', 'sysname', 'sp_who',
- 'sysobjects', 'sp_', 'sysprocesses', 'master', 'sys', 'db_',
- 'is_', 'exec', 'end', 'xp_', '; --', 'alter', 'begin', 'cursor',
- 'kill', '--', 'tabname', 'sys'];
Visual Studio 2015: You can download it from here.
Code Part
Step 1: Create a sample database and Table for testing this application. Here is a SQL script to create database and table with insert query. Kindly run the below code in your SQL Server to create DB and Tables.
- ---- =============================================
- ---- Author : Shanu
- ---- Create date : 2016-05-12
- ---- Description : To Create Database,Table and Sample Insert Query
- ---- Latest
- ---- Modifier : Shanu
- ---- Modify date : 2016-05-12
- ---- =============================================
- ----Script to create DB,Table and sample Insert data
- USE MASTER
- GO
- --1) Check for the Database Exists .If the database is exist then drop and create new DB
- IF EXISTS (SELECT [name] FROM sys.databases WHERE [name] = 'DashboardDB' )
- DROP DATABASE DashboardDB
- GO
- CREATE DATABASE DashboardDB
- GO
- USE DashboardDB
- GO
- -- 1) //////////// ItemDetails table
- -- Create Table ItemDetails,This table will be used to store the details like Item Information
- IF EXISTS ( SELECT [name] FROM sys.tables WHERE [name] = 'ItemDetail' )
- DROP TABLE ItemDetail
- GO
- CREATE TABLE [dbo].[ItemDetail](
- [ID] [int] IDENTITY(1,1) NOT NULL,
- [ItemNo] [varchar](100) NOT NULL ,
- [ItemName] [varchar](100) NOT NULL,
- [Comments] [varchar](100) NOT NULL,
- [Price] INT NOT NULL,
- PRIMARY KEY CLUSTERED
- (
- [ID] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
- GO
- Insert into ItemDetail(ItemNo,ItemName,Comments,Price) values
- ('101','NoteBook', 'HP Notebook 15 Inch', 24500)
- Insert into ItemDetail(ItemNo,ItemName,Comments,Price) values
- ('102','MONITOR', 'SAMSNG', '8500')
- Insert into ItemDetail(ItemNo,ItemName,Comments,Price) values
- ('103','MOBILE', 'SAMSUNG NOTE 5', 42500)
- Insert into ItemDetail(ItemNo,ItemName,Comments,Price) values
- ('104','MOBILE', 'SAMSUNG S7 Edge', 56000)
- Insert into ItemDetail(ItemNo,ItemName,Comments,Price) values
- ('105','MOUSE', 'ABKO', 780)
- Insert into ItemDetail(ItemNo,ItemName,Comments,Price) values
- ('106','HDD' ,'LG', 3780)
- select * from ItemDetail
- select ItemName,SUM(convert(int,Price)) as totalCost
- from ItemDetail
- GROUP BY ItemName
- -- 2) User table
- IF EXISTS ( SELECT [name] FROM sys.tables WHERE [name] = 'UserDetails' )
- DROP TABLE UserDetails
- GO
- CREATE TABLE [dbo].UserDetails(
- [UserID] [int] IDENTITY(1,1) NOT NULL,
- [UserName] [varchar](100) NOT NULL,
- [UserType] [varchar](100) NOT NULL,
- [Phone] [varchar](20) NOT NULL,
- PRIMARY KEY CLUSTERED
- (
- [UserID] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
- Insert into UserDetails(UserName,UserType,Phone) values
- ('SHANU','Admin','01039124503')
- Insert into UserDetails(UserName,UserType,Phone) values
- ('Afraz','user','01039120984')
- Insert into UserDetails(UserName,UserType,Phone) values
- ('Afreen','user','01039120005')
- Insert into UserDetails(UserName,UserType,Phone) values
- ('Raj','Admin','01039120006')
- Insert into UserDetails(UserName,UserType,Phone) values
- ('Mak','Manager','01039124567')
- Insert into UserDetails(UserName,UserType,Phone) values
- ('Jack','Manager','01039120238')
- Insert into UserDetails(UserName,UserType,Phone) values
- ('Pak','User','01039125409')
- Insert into UserDetails(UserName,UserType,Phone) values
- ('Ninu','Accountant','01039126810')
- Insert into UserDetails(UserName,UserType,Phone) values
- ('Nanu','Accountant','01039152011')
- -- select * from Userdetails
- -- 3 UserAddress
- IF EXISTS ( SELECT [name] FROM sys.tables WHERE [name] = 'UserAddress' )
- DROP TABLE UserAddress
- GO
- CREATE TABLE [dbo].UserAddress(
- [UserAddID] [int] IDENTITY(1,1) NOT NULL,
- [UserID] [int] ,
- [Address] [varchar](200) NOT NULL,
- [Email] [varchar](100) NOT NULL,
- PRIMARY KEY CLUSTERED
- (
- [UserAddID] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
- Insert into UserAddress(UserID,Address,Email) values
- (1,'Madurai,Tamil Nadu, India','[email protected]')
- Insert into UserAddress(UserID,Address,Email) values
- (2,'Madurai,Tamil Nadu, India','[email protected]')
- Insert into UserAddress(UserID,Address,Email) values
- (3,'Seoul,South Korea','[email protected]')
- select * from UserAddress
- select A.UserName,A.UserType,A.Phone,B.Address,B.Email
- From
- Userdetails A Left Outer JOIN UserAddress B
- on
- A.UserID=B.UserID
This is our main stored procedure used to run all our Dynamic SQL Select query and return the result to bind in our MVC page.
- USE [DashboardDB]
- GO
- /****** Object: StoredProcedure [dbo].[USP_Dashboard_Select] ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- -- 1) select top 10 random kidsLearnerMaster records
- -- Author : Shanu
- -- Create date : 2016-05-14
- -- Description :To run dymanic Query
- -- Tables used : Dynamic Table
- -- Modifier : Shanu
- -- Modify date : 2016-05-14
- -- =============================================
- -- To Select all user roles
- -- EXEC USP_Dashboard_Select @columnName = 'UserName,UserType,Phone' ,@TableNames = 'UserDetails' ,@isCondition=0,@ConditionList='UserType=''ADMIN'' ',@isGroupBY =1,@GroupBYList = 'UserName,UserType,Phone', @isOrderBY =1,@OrderBYList = ' UserType '
- -- EXEC USP_Dashboard_Select @columnName = 'ItemName,SUM(Price) as totalCost' ,@TableNames = 'ItemDetail' ,@isCondition=0,@ConditionList='Price>''400'' ',@isGroupBY =1,@GroupBYList = 'ItemName'
- -- EXEC USP_Dashboard_Select @sqlQuery = 'Select * from ItemDetail'
- -- EXEC USP_Dashboard_Select @sqlQuery = 'select ID,ItemNo ,ItemName ,Comments ,Price from ItemDetail'
- -- =============================================
- ALTER PROCEDURE [dbo].[USP_Dashboard_Select]
- (
- @sqlQuery varchar(MAX)='',
- @columnName varchar(MAX)='',
- @TableNames varchar(MAX)='',
- @isCondition INT=0,
- @ConditionList varchar(MAX)='',
- @isGroupBY INT=0,
- @GroupBYList varchar(MAX)='',
- @isOrderBY INT=0,
- @OrderBYList varchar(MAX)=''
- )
- AS
- BEGIN
- BEGIN TRY
- IF @sqlQuery =''
- BEGIN
- SET @sqlQuery = 'SELECT ' + @columnName + ' FROM ' + @TableNames
- IF @isCondition=1
- BEGIN
- SET @sqlQuery = @sqlQuery+ ' WHERE ' + @ConditionList
- END
- IF @isGroupBY=1
- BEGIN
- SET @sqlQuery = @sqlQuery+ ' GROUP BY ' + @GroupBYList
- END
- IF @isOrderBY=1
- BEGIN
- SET @sqlQuery = @sqlQuery+ ' Order BY ' + @OrderBYList
- END
- EXEC (@sqlQuery)
- END
- ELSE
- BEGIN
- EXEC (@sqlQuery)
- END
- END TRY
- BEGIN CATCH
- SELECT ERROR_NUMBER() AS ErrorNumber
- ,ERROR_MESSAGE() AS ErrorMessage;
- END CATCH
- END
After installing our Visual Studio 2015, click Start, then Programs and select Visual Studio 2015 - Click Visual Studio 2015. Click New, then Project, select Web and then select ASP.NET Web Application. Enter your project name and click OK.

Now we have created our MVC Application. Next, we add our connection string in our Web.Config file. Here, we are not using Entity Framework. We will directly get the data from our MVC Web API controller method using normal ADO.NET method.
- <add name="dashboard" connectionString="Data Source=SQLSERVERNAME;Initial Catalog=DashboardDB;Persist Security Info=True;User ID=UID;Password=PWD" providerName="System.Data.SqlClient" />
Step 3: Add web API Controller
Right click Controllers folder, click Add and then Controller.

Here, we will add a Web API Controller to be used for our AngularJS.
Select Web API 2 Controller – Empty and click Add. After that enter the controller name as DashboardAPIController

Get Method

Alice NguyenPosted Nov 30, 2018, 8:48 AM
Thanks for sharing, great article.
Bikesh SrivastavaPosted Aug 23, 2016, 5:28 AM
Very nice
Syed ShanuPosted Jun 10, 2016, 12:28 AM
Thank You again to all
Pradeep SahooPosted May 26, 2016, 7:07 AM
Nice share .....
Muhammad Aqib ShehzadPosted May 23, 2016, 4:00 AM
very nice article
Gowtham KPosted May 23, 2016, 2:23 AM
Great Article, Thanks for sharing
Syed ShanuPosted May 22, 2016, 7:47 PM
Thank You Again all
Munesh SharmaPosted May 22, 2016, 10:54 AM
nice sharing
Humayun Kabir MamunPosted May 22, 2016, 7:11 AM
Nice...
Kuppurasu NagarajPosted May 22, 2016, 5:17 AM
Nice Sharing..
Santosh Kumar AdidawarpuPosted May 21, 2016, 10:49 AM
Nice one..
Vignesh ManiPosted May 21, 2016, 9:19 AM
nice one
Syed ShanuPosted May 21, 2016, 8:40 AM
Thank You All
Neeraj KumarPosted May 20, 2016, 1:49 PM
Good one
Debasis SahaPosted May 20, 2016, 10:47 AM
Nice One..
Thiruppathi RPosted May 20, 2016, 9:34 AM
Great One...
Prasanna MuraliPosted May 20, 2016, 8:28 AM
Nice one..
Anu VPosted May 20, 2016, 7:49 AM
Nice
Mohammed IbrahimPosted May 20, 2016, 6:47 AM
nice