Thursday, July 26, 2018

li ng-repeat example




<ul>
        <li ng-repeat="control in controls" id="{{control.id}}">
        </li>
</ul>

Thursday, February 18, 2016

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http:// localhost' is therefore not allowed access.

AngularJS which calls an API- WebMethod for json data. When cross domain origin problem got this error:
Error: 
XMLHttpRequest cannot load api
 No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '.....' is therefore not allowed access.

Solution: 

Add to web.config:


    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Content-Type, accessToken, signedInUserId, x-requested-with" />
      </customHeaders>
    </httpProtocol>

XMLHttpRequest cannot load Request header field dataType is not allowed by Access-Control-Allow-Headers in preflight response.

Error:

When POST to WebMethod - ASP.NET

XMLHttpRequest cannot load Request header field dataType is not allowed by Access-Control-Allow-Headers in preflight response.

Solution: 

Add to web.config:


    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Content-Type, accessToken, signedInUserId, x-requested-with" />
      </customHeaders>
    </httpProtocol>

Tuesday, February 9, 2016

Angular ASP.net SQL Server Tutorial

Angular ASP.net SQL Server Tutorial


Database

USE [PM]
GO

/****** Object: Table [dbo].[Projects] Script Date: 09/02/2016 14:58:24 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Projects] (
    [ProjectID]          INT            IDENTITY (1, 1) NOT NULL,
    [ProjectName]        VARCHAR (50)   NOT NULL,
    [IsActive]           BIT            NOT NULL,
    [ProjectDescription] NVARCHAR (MAX) NULL,
    [CreatedDate]        DATETIME       NULL
);


Web.config

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <connectionStrings>
    <add name ="DB" connectionString="Data Source=(localdb)\ProjectsV12;Initial Catalog=PM;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;"/>
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>

</configuration>

Index.html


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>PM-Home</title>
</head>
<body>
    <a href="modules/projects/client/views/projects.html">Projects</a>
</body>

</html>

project.js

var app = angular.module("ProjectModule", []);
app.controller("ProjectCntrl", function ($scope, $http) {
    $scope.projectorder = "ProjectID";
    $scope.ProjectName = "";
    $scope.Save = function () {
        var httpreq = {
            method: 'POST',
            url: '../../../projects/server/projects.aspx/Save',
            headers: {
                'Content-Type': 'application/json; charset=utf-8',
                'dataType': 'json'
            },
            data: { ProjectName: $scope.ProjectName, ProjectDescription: $scope.ProjectDescription }
        }
        $http(httpreq).success(function (response) {
            $scope.fillProjects();
            alert("Saved successfully.");
        })
    };
    $scope.Delete = function (SID) {
        if (confirm("Are you sure want to delete?")) {
            var httpreq = {
                method: 'POST',
                url: '../../../projects/server/projects.aspx/Delete',
                headers: {
                    'Content-Type': 'application/json; charset=utf-8',
                    'dataType': 'json'
                },
                data: { ProjectID: SID }
            }
            $http(httpreq).success(function (response) {
                $scope.fillProjects();
                alert("Deleted successfully.");
            })
        }
    };
    $scope.fillProjects = function () {
        $scope.ProjectName = "";
        var httpreq = {
            method: 'POST',
            url: '../../../projects/server/projects.aspx/GetProjects',
            headers: {
                'Content-Type': 'application/json; charset=utf-8',
                'dataType': 'json'
            },
            data: {}
        }
        $http(httpreq).success(function (response) {
            $scope.ProjectList = response.d;
        })
    };
  
    $scope.fillProjects();
});

projects.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js"></script>
    <title>Projects</title>
    <style>
        table, th, td {
            border: 1px solid grey;
            border-collapse: collapse;
            padding: 5px;
        }

            table th {
                background-color: #274257;
                color: #fff;
            }

            table tr:nth-child(odd) {
                background-color: #f1f1f1;
            }

            table tr:nth-child(even) {
                background-color: #ffffff;
            }
    </style>
   
    <script src="../js/project.js"></script>

</head>
<body>
    <form id="form1">
        New Project
        <div ng-app="ProjectModule" ng-controller="ProjectCntrl">
            <table>
                <tr>
                    <td>
                        Name:
                    </td>
                    <td>
                        <input type="text" id="txtProjectName" ng-model="ProjectName" />
                    </td>
                </tr>
                <tr>
                    <td style="text-align: right;">
                        Description:
                    </td>
                    <td>
                        <textarea id="txtProjectDesc" cols="20" rows="2" ng-model="ProjectDescription"></textarea>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <input type="button" value="Save" ng-click="Save()" />
                    </td>
                </tr>
            </table>
            <hr/>
            Filter:<input type="text" ng-model="search" />
            <hr/>
            <table>
                <thead>
                    <tr>
                        <th>
                            Id
                        </th>
                        <th>
                            Name
                        </th>
                        <th>
                            Description
                        </th>
                        <th>
                            Created Date
                        </th>
                        <th>
                        </th>
                    </tr>
                </thead>
                <tr ng-repeat="project in ProjectList | orderBy : projectorder | filter:search ">
                    <td ng-bind="project.ProjectID"></td>
                    <td ng-bind="project.ProjectName"></td>
                    <td ng-bind="project.ProjectDescription"></td>
                    <td ng-bind="project.CreatedDate"></td>
                    <td>
                        <a href="#" ng-click="Delete(project.ProjectID)">Delete</a>
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

projects.cs

namespace PM.modules.projects.model
{
    public class Projects
    {
        public int ProjectID;
        public string ProjectName;
        public string ProjectDescription;
        public string CreatedDate;

        public Projects(int _ProjectID, string _ProjectName, string _ProjectDescription, string _CreatedDate)
        {
            ProjectID = _ProjectID;
            ProjectName = _ProjectName;
            ProjectDescription = _ProjectDescription;
            CreatedDate = _CreatedDate;
        }
    }
}

projects.aspx

using PM.modules.projects.model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;

namespace PM.modules.projects.server
{
    public partial class projects : System.Web.UI.Page
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [System.Web.Services.WebMethod()]
        public static List<Projects> GetProjects()
        {
            List<Projects> projects = new List<Projects>();
            DataSet ds = new DataSet();
            using (SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["DB"].ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = con;
                    cmd.CommandText = "select ProjectID, ProjectName, ProjectDescription, CreatedDate from Projects where IsActive=1;";
                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    {
                        da.Fill(ds);
                    }
                }
            }
            if (ds != null && ds.Tables.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                    projects.Add(new Projects(int.Parse(dr["ProjectID"].ToString()), dr["ProjectName"].ToString(), dr["ProjectDescription"].ToString(), dr["CreatedDate"].ToString()));
            }
            return projects;
        }

        [System.Web.Services.WebMethod()]
        public static void Save(string ProjectName, string ProjectDescription)
        {
            using (SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["DB"].ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = con;
                    cmd.CommandText = "insert into Projects (ProjectName,ProjectDescription) values (@ProjectName, @ProjectDescription);";
                    cmd.Parameters.AddWithValue("@ProjectName", ProjectName);
                    cmd.Parameters.AddWithValue("@ProjectDescription", ProjectDescription);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
        }

        [System.Web.Services.WebMethod()]
        public static void Delete(int ProjectID)
        {
            using (SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["DB"].ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = con;
                    cmd.CommandText = "delete from Projects where ProjectID=@ProjectID;";
                    cmd.Parameters.AddWithValue("@ProjectID", ProjectID);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
        }

    }
}