Introduction
In this article, we will learn MVC, using Angular wizard & datatable from the Server side with the stored procedure, using Visual Studio 2015
Why use Wizard?
Wizard means the process is moved by one-by-one step. It allows logically dividing the group of data.That way, the user can enter valuable data.
In this article, we are going to
- Create a database.
- Create a stored procedure.
- Create MVC Application.
- Using Angular Datatable.
Create a database
Open SQL Server 2016. Click "New Query" window & run the query given below.
- USE [master]
- GO
- CREATE DATABASE [test]
- CONTAINMENT = NONE
- ON PRIMARY
- ( NAME = N'test', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\test.mdf' , SIZE = 8192KB , MAXSIZE = UNLIMITED, FILEGROWTH = 65536KB ),
- FILEGROUP [DocFiles] CONTAINS FILESTREAM DEFAULT
- ( NAME = N'FileStream', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\FileStream' , MAXSIZE = UNLIMITED)
- LOG ON
- ( NAME = N'test_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\test_log.ldf' , SIZE = 8192KB , MAXSIZE = 2048GB , FILEGROWTH = 65536KB )
- GO
- ALTER DATABASE [test] SET COMPATIBILITY_LEVEL = 130
- GO
- IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
- begin
- EXEC [test].[dbo].[sp_fulltext_database] @action = 'enable'
- end
- GO
- ALTER DATABASE [test] SET ANSI_NULL_DEFAULT OFF
- GO
- ALTER DATABASE [test] SET ANSI_NULLS OFF
- GO
- ALTER DATABASE [test] SET ANSI_PADDING OFF
- GO
- ALTER DATABASE [test] SET ANSI_WARNINGS OFF
- GO
- ALTER DATABASE [test] SET ARITHABORT OFF
- GO
- ALTER DATABASE [test] SET AUTO_CLOSE OFF
- GO
- ALTER DATABASE [test] SET AUTO_SHRINK OFF
- GO
- ALTER DATABASE [test] SET AUTO_UPDATE_STATISTICS ON
- GO
- ALTER DATABASE [test] SET CURSOR_CLOSE_ON_COMMIT OFF
- GO
- ALTER DATABASE [test] SET CURSOR_DEFAULT GLOBAL
- GO
- ALTER DATABASE [test] SET CONCAT_NULL_YIELDS_NULL OFF
- GO
- ALTER DATABASE [test] SET NUMERIC_ROUNDABORT OFF
- GO
- ALTER DATABASE [test] SET QUOTED_IDENTIFIER OFF
- GO
- ALTER DATABASE [test] SET RECURSIVE_TRIGGERS OFF
- GO
- ALTER DATABASE [test] SET DISABLE_BROKER
- GO
- ALTER DATABASE [test] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
- GO
- ALTER DATABASE [test] SET DATE_CORRELATION_OPTIMIZATION OFF
- GO
- ALTER DATABASE [test] SET TRUSTWORTHY OFF
- GO
- ALTER DATABASE [test] SET ALLOW_SNAPSHOT_ISOLATION OFF
- GO
- ALTER DATABASE [test] SET PARAMETERIZATION SIMPLE
- GO
- ALTER DATABASE [test] SET READ_COMMITTED_SNAPSHOT OFF
- GO
- ALTER DATABASE [test] SET HONOR_BROKER_PRIORITY OFF
- GO
- ALTER DATABASE [test] SET RECOVERY FULL
- GO
- ALTER DATABASE [test] SET MULTI_USER
- GO
- ALTER DATABASE [test] SET PAGE_VERIFY CHECKSUM
- GO
- ALTER DATABASE [test] SET DB_CHAINING OFF
- GO
- ALTER DATABASE [test] SET FILESTREAM( NON_TRANSACTED_ACCESS = FULL, DIRECTORY_NAME = N'DocFileDirctory' )
- GO
- ALTER DATABASE [test] SET TARGET_RECOVERY_TIME = 60 SECONDS
- GO
- ALTER DATABASE [test] SET DELAYED_DURABILITY = DISABLED
- GO
- EXEC sys.sp_db_vardecimal_storage_format N'test', N'ON'
- GO
- ALTER DATABASE [test] SET QUERY_STORE = OFF
- GO
Create a table
I will create a new table that is based on the booking information.
- CREATE TABLE [dbo].[WizardMaster](
- [BookCode] [varchar](10) NULL,
- [BookName] [varchar](50) NULL,
- [BookDesc] [varchar](500) NULL,
- [BookAuthor] [varchar](50) NULL,
- [Email] [varchar](50) NULL,
- [Password] [varchar](10) NULL,
- [Name] [varchar](10) NULL,
- [PhoneNo] [varchar](10) NULL,
- [Addess] [varchar](500) NULL
- ) ON [PRIMARY]
- GO
After creating the table, add some data, as given below.
Create a stored procedure
I have written the stored procedure for my data operations, so run SP as given below. In this procedure, I have done "Select" and "Insert" operations.
Create MVC Application
Open Visual Studio 2015.
Go to Menu, click New->New project ->select Visual C# under templates-> ASP.NET Web Application.
Once you click OK button, one more window opens. Select MVC.
HTML Design
Step 1
Here, getting email input & matching password using "ui-validate-watch" & "ui-validate".
- <div ng-show="activeStep==1" class="wizard-step">
- <h4> Create Account <small>— Step 1</small> </h4>
- <form name="form_step1" class="form-validate">
- <p>Email:</p> <input type="email" placeholder="[email protected]" name="email" ng-model="Email" required="required" class="form-control" />
- <p class="mt">Password</p>
- <div class="row">
- <div class="col-xs-6"> <input id="id-source" type="password" placeholder="Type your password" ng-model="form.match1" class="form-control" /> </div>
- <div class="col-xs-6"> <input type="password" placeholder="Retype your password" name="confirm_match" data-parsley-equalto="#id-source" required="" ng-model="form.match2" ui-validate="'$value==form.match1'" ui-validate-watch="'validator.match1'" class="form-control" /> </div>
- </div>
- <p ng-show="form_step1.confirm_match.$error.validator" class="text-danger">Password do not match!</p>
- <div class="mt"> <button type="submit" ng-disabled="form_step1.$invalid" ng-click="activeStep=2" class="btn btn-primary">Next</button> </div>
- </form>
- </div>
Output 1
setp 1 output given below
Step 2Once you click "Next" button.you have completed step 1. it will move the step 2. In this step i have used ng-pattern="/^[0-9]+$/" & required key word using for validation .in html form element.
- <div ng-show="activeStep==2" class="wizard-step">
- <h4> Your Social Networks <small>— Step 2</small> </h4>
- <form name="form_step2" class="form-validate">
- <p class="mt">Name:</p> <input type="text" name="name" placeholder="Your fullname" ng-model="name" required="" class="form-control" />
- <p class="mt">Phone:</p> <input type="text" name="phone" ng-model="phone" placeholder="Only numbers" ng-pattern="/^[0-9]+$/" class="form-control" />
- <p class="mt">Address:</p> <textarea name="address" placeholder="Your address" ng-model="address" class="form-control"></textarea>
- <div class="mt"> <button type="button" ng-click="activeStep=1" class="btn btn-default">Prev</button> <button type="submit" ng-disabled="form_step2.$invalid" ng-click="activeStep=3" class="btn btn-primary">Next</button> </div>
- </form>
- </div>
Output 2
if you need go back,click to "Prev" button.step 1 has hide,but our assigned values will follow in the state.
Step 3
After clicking the "Next" button in step 2.it will show as step 3.
- <div ng-show="activeStep==3" class="wizard-step">
- <h4> Personal details <small>— Step 3</small> </h4>
- <form name="form_step3" class="form-validate">
- <div class="form-group">
- <p class="mt">Book Code:</p> <input type="text" class="form-control" ng-model="BookCode" placeholder="Book Code" /> </div>
- <div class="form-group">
- <p class="mt">Book Name:</p> <input type="text" class="form-control" ng-model="BookName" placeholder="Book Name" /> </div>
- <div class="form-group">
- <p class="mt">Book Desc:</p> <input type="text" class="form-control" ng-model="BookDesc" placeholder="Book Desc" /> </div>
- <div class="form-group">
- <p class="mt">Book Author:</p> <input type="text" class="form-control" ng-model="BookAuthor" placeholder="Book Author" /> </div>
- </form>
- <div ng-show="stepsDone" class="alert alert-success mv-lg">
- <p class="text-bold m0">Excellent ! You've completed all steps.</p>
- </div>
- <div class="mt"> <button type="button" ng-click="activeStep=2" class="btn btn-default">Prev</button> <button type="button" ng-disabled="form_step3.$invalid" ng-click="Save()" class="btn btn-primary">Finish</button> </div>
- </div>
Output 3
If you click "Finish" button,data should add in tables.
Angular Module
angular.module('uiroute',['ui.router', 'datatables']);
Create & design HTML page with the table. Mention datatable=ng. Now, bind the Server data.
- <table datatable="ng" class="table-responsive table-bordered table-striped ">
- <thead style="background-color:#428bca;color:white">
- <tr>
- <td> User Name </td>
- <td> Email </td>
- <td> Book Name </td>
- <td> Book Description </td>
- <td> Book Author Name </td>
- <td> Phone No </td>
- <td> Address </td>
- </tr>
- </thead>
- <tbody>
- <tr ng-repeat="model in LoadData">
- <td>{{ model.Name }}</td>
- <td>{{ model.Email }}</td>
- <td>{{ model.BookName }}</td>
- <td>{{ model.BookDesc }}</td>
- <td>{{ model.BookAuthor }}</td>
- <td>{{ model.Phone }}</td>
- <td>{{ model.Address }}</td>
- </tr>
- </tbody>
- </table>
Code Behind
Create Model folder in Solution Explorer & create new class in Model folder.
- public class BookModel {
- public string Email {
- get;
- set;
- }
- public string Password {
- get;
- set;
- }
- public string Name {
- get;
- set;
- }
- public string Phone {
- get;
- set;
- }
- public string Address {
- get;
- set;
- }
- public string BookCode {
- get;
- set;
- }
- public string BookName {
- get;
- set;
- }
- public string BookDesc {
- get;
- set;
- }
- public string BookAuthor {
- get;
- set;
- }
- public string Mode {
- get;
- set;
- }
- }
Write the method given below in home controller. LoadData displays the data in the Datatable & another one is using data manipulation.
- [HttpPost]
- #region LoadData
- public JsonResult LoadData(BookModel Param) {
- List < BookModel > BookList = new List < BookModel > ();
- using(var con = new SqlConnection(ConfigurationManager.ConnectionStrings["DbSqlCon"].ConnectionString)) {
- var cmd = new SqlCommand("WizardBook_SP", con);
- cmd.CommandType = CommandType.StoredProcedure;
- cmd.Parameters.Add(new SqlParameter("@Mode", SqlDbType.VarChar)).Value = Param.Mode;
- try {
- con.Open();
- using(SqlDataReader DbReader = cmd.ExecuteReader())
- if (DbReader.HasRows) {
- while (DbReader.Read()) {
- BookModel Books = new BookModel();
- Books.Email = DbReader.GetString(DbReader.GetOrdinal("Email"));
- Books.Password = DbReader.GetString(DbReader.GetOrdinal("Password"));
- Books.Name = DbReader.GetString(DbReader.GetOrdinal("Name"));
- Books.Phone = DbReader.GetString(DbReader.GetOrdinal("PhoneNo"));
- Books.BookCode = DbReader.GetString(DbReader.GetOrdinal("BookCode"));
- Books.BookName = DbReader.GetString(DbReader.GetOrdinal("BookName"));
- Books.BookDesc = DbReader.GetString(DbReader.GetOrdinal("BookDesc"));
- Books.BookAuthor = DbReader.GetString(DbReader.GetOrdinal("BookAuthor"));
- Books.Address = DbReader.GetString(DbReader.GetOrdinal("Addess"));
- BookList.Add(Books);
- }
- }
- return Json(BookList, JsonRequestBehavior.AllowGet);
- } finally {
- con.Close();
- }
- }
- }#
- endregion[HttpPost]# region EditData
- public string EditData(BookModel Param) {
- if (Param != null) {
- using(var con = new SqlConnection(ConfigurationManager.ConnectionStrings["DbSqlCon"].ConnectionString)) {
- var cmd = new SqlCommand("WizardBook_SP", con);
- cmd.CommandType = CommandType.StoredProcedure;
- cmd.Parameters.Add(new SqlParameter("@Mode", SqlDbType.VarChar)).Value = Param.Mode;
- cmd.Parameters.Add(new SqlParameter("@Phone", SqlDbType.VarChar)).Value = Param.Phone;
- cmd.Parameters.Add(new SqlParameter("@Name", SqlDbType.VarChar)).Value = Param.Name;
- cmd.Parameters.Add(new SqlParameter("@Email", SqlDbType.VarChar)).Value = Param.Email;
- cmd.Parameters.Add(new SqlParameter("@PassWord", SqlDbType.VarChar)).Value = Param.Password;
- cmd.Parameters.Add(new SqlParameter("@BookCode", SqlDbType.VarChar)).Value = Param.BookCode;
- cmd.Parameters.Add(new SqlParameter("@BookName", SqlDbType.VarChar)).Value = Param.BookName;
- cmd.Parameters.Add(new SqlParameter("@BookDesc", SqlDbType.VarChar)).Value = Param.BookDesc;
- cmd.Parameters.Add(new SqlParameter("@BookAutor", SqlDbType.VarChar)).Value = Param.BookAuthor;
- cmd.Parameters.Add(new SqlParameter("@Address", SqlDbType.VarChar)).Value = Param.Address;
- try {
- con.Open();
- cmd.ExecuteNonQuery();
- return "Success";
- } catch (Exception ex) {
- return ex.ToString();
- } finally {
- if (con.State != ConnectionState.Closed) con.Close();
- }
- }
- } else {
- return "Model Error";
- }
- }#endregion
Declare the connection string in Web.config file.
- <connectionStrings>
- <add name="DbSqlCon" connectionString="Data Source=xxxx; Initial Catalog=test; User Id=sa; Password=XXX; connect timeout=0;" providerName="System.Data.SqlClient;" />
- </connectionStrings>
Create an Angular Controller & Service to get the data from the Server side.
Angular Controller
- controller('WizardController', function($scope, WizardService) {
- $scope.loadTable = function() {
- var Param = {
- Mode: 'GET'
- }
- var ServiceData = WizardService.loadGrid(Param);
- ServiceData.then(function(result) {
- $scope.LoadData = result.data;
- }, function() {});
- }
- $scope.loadTable();
- $scope.Save = function() {
- debugger;
- var Param = {
- Mode: 'ADD',
- Email: $scope.Email,
- Password: $scope.form.match1,
- Name: $scope.name,
- Phone: $scope.phone,
- Address: $scope.address,
- BookCode: $scope.BookCode,
- BookName: $scope.BookName,
- BookDesc: $scope.BookDesc,
- BookAuthor: $scope.BookAuthor
- }
- var ServiceData = WizardService.EditData(Param);
- ServiceData.then(function(result) {
- $scope.loadTable();
- $scope.stepsDone = true;
- }, function() {});
- }
- });
Angular Service
- this.loadGrid = function(Param) {
- var response = $http({
- method: "post",
- url: "Home/LoadData",
- data: JSON.stringify(Param),
- dataType: "json"
- });
- return response;
- }
- this.EditData = function(Param) {
- var response = $http({
- method: "post",
- url: "Home/EditData",
- data: JSON.stringify(Param),
- dataType: "json"
- });
- return response;
- }
Do not forget to refer the plug in files and JS file also.
Plug In
- <link href="~/Plugin/datatables/media/css/jquery.dataTables.min.css" rel="stylesheet" />
- <script src="~/Plugin/datatables/media/js/jquery.dataTables.js"></script>
- <script src="~/Plugin/angular-datatables/dist/angular-datatables.js"></script>
My Files
- <script src="~/App/App.module.js"></script>
- <script src="~/App/App.config.js"></script>
- <script src="~/App/BookController.js"></script>
Once you are done with the process given above, your datatable is ready to load. Thus, run the Application.
Output 5
Here, I have done simple angular wizard method.
Kindly refer my AngularJS articles
Conclusion
In this article, we have learned MVC, using Angular wizard. If you have any queries, please tell me through the comments section. Your comments are very valuable.
Happy Coding.....