Monday, January 3, 2022

Angular Get Post in MVC

Angular Js or 1 : Get Post Request

1. Model


public class admin_support_model
{
    public int? id { get; set; }
    public string name { get; set; }
    public string ed_code { get; set; }
    public string email { get; set; }
    public string phone { get; set; }
    public string remark { get; set; }

}
public class education
{
    public string ed_code { get; set; }
    public string ed_name { get; set; }
}
public class admin_news_letter_model
{
    public int? id { get; set; }
    public string email { get; set; }
    public Nullable<DateTime> edate { get; set; }
}

2. Views


@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>

    <script src="/Scripts2/jquery-3.3.1.min.js"></script>
    <script src="/Scripts2/angular.js"></script>
    <script src="/AngularCtrls/CommonFiles/myApp.js"></script>
</head>
<body  ng-app="MyApp">
    <script src="~/AngularCtrls/Admin/supportToAdminCtrl.js"></script>
    <div  ng-controller="supportToAdminCtrl">
        <form id="contact" ng-submit="af_SupportPost();" autocomplete="off">
            <div class="heading-title left">
                <small class="iq-font-green">Support With Us</small>
                <h2 class="title iq-tw-6">Support</h2>

            </div>
            <div class="contact-form">
                <div class="section-field">
                    <br />
                    <input class="require" id="cname" type="text" ng-model="p.name" placeholder="Name*" name="cname" required>
                </div>
                <div class="section-field">
                    Education
                    <select  id="ed_code" name="ed_code" ng-model="p.ed_code" required>
                        <option>
                        </option>
                        <option ng-repeat="m in educationList" value="{{m.ed_code}}" style="width:100px;">
                            {{m.ed_name}}
                        </option>
                        <input type="search" id="ed_name" ng-model="p.ed_name" ng-change="af_Education_List()" style="width:40px;"  />
                    </select>
                </div>
                <div class="section-field">
                    <input class="require" id="cemail" type="email" ng-model="p.email" placeholder="Email*" name="cemail" required>
                </div>
                <div class="section-field">
                    <input class="require" id="cphone" type="number" ng-model="p.phone" placeholder="Phone*" name="cphone" required>
                </div>
                <div class="section-field textarea">
                    <textarea id="cmessage" class="input-message require" ng-model="p.remark" placeholder="Comment*" rows="5" name="cmessage" required></textarea>
                </div>
                <div class="d-block text-center">
                    <button id="btnsupport" type="submit" value="Send" class="button iq-mt-15">Send Message</button>
                </div>
            </div>
        </form>


        <form id="contact" ng-submit="af_WeeklyNews()" autocomplete="off">
            <div class="heading-title text-center">
                <h2 class="title iq-tw-6" style="margin-bottom: 15px;">
                    Weekly newsletter
                </h2>
            </div>

            <div class="contact-form">
                <div class="row">
                    <div class="col-md-12">
                        <div class="section-field">
                            <input class="require" id="contact_email" type="email" ng-model="p.email" placeholder="Email*" name="contact_email" required>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-4">
                        <div class="d-block text-center">
                            <input id="Subscribesubmit" name="submit" type="submit" value="Subscribe" class="button" />
                        </div>
                    </div>
                </div>
            </div>            
        </form>
        <br />
        <table>
            <tr>
                <th>Srno</th>
                <th>Name</th>
                <th>Mobile</th>
                <th>Email</th>                
            </tr>
            <tr ng-repeat="m in supportList">                
                <td>{{m.id}}</td>
                <td>{{m.name}}</td>
                <td>{{m.phone}}</td>
                <td>{{m.email}}</td>                
            </tr>
        </table>

    </div>
</body>
</html>


    JsDownload Link

    https://drive.google.com/file/d/1cRFTMjag3x_fFTRPdPKoArRwDnzHo89z/view?usp=sharing

3. MVC Controller

public class HomeController : Controller
{
    // GET: Home
        public ActionResult Index()
        {
            return View();
        }
        string sql = "";    
        public ActionResult WeeklyNews(admin_news_letter_model d)
        {
            msg msg = new msg() { message = "News Letter Success submitted.", priority = "success", title = "Message" };            
            return Json(new { msg = msg }, JsonRequestBehavior.AllowGet);
        }
        public ActionResult SupportPost(admin_support_model d)
        {
            msg msg = new msg() { message = "Your Query Successfully submitted.", priority = "success", title = "Message" };            
            return Json(new { msg = msg }, JsonRequestBehavior.AllowGet);
        }
        public ActionResult SupportList(admin_support_model d)
        {
            admin_support_model_mvv mvv = new admin_support_model_mvv();

            msg msg = new msg() { message = "Your Query Successfully submitted.", priority = "success", title = "Message" };
            mvv.msgs = msg;

            List<admin_support_model> list = new List<admin_support_model>();
            list.Add(new admin_support_model { id = 1, name = "Rinkesh Gola", phone = "789475897", email = "abc@gmail.com", remark = "aass" });
            list.Add(new admin_support_model { id = 2, name = "Ramesh Sing", phone = "7894712345", email = "abc2@gmail.com", remark = "aass" });
            list.Add(new admin_support_model { id = 3, name = "Ajay Saagar", phone = "789476789", email = "abc3@gmail.com", remark = "aass" });
            list.Add(new admin_support_model { id = 4, name = "Manoj AAAA", phone = "7894711111", email = "abc32@gmail.com", remark = "aass" });
            list.Add(new admin_support_model { id = 5, name = "Sanjay Sharma", phone = "7894744444", email = "abca@gmail.com", remark = "aass" });

            mvv.admin_support_models_list = list;

            return Json(new { modelData = mvv }, JsonRequestBehavior.AllowGet);
        }
        public ActionResult ed_code_list(education d)
        {
            List<education> list = new List<education>();
            list.Add(new education { ed_code = "ED01", ed_name = "Illiterate " });
            list.Add(new education { ed_code = "ED02", ed_name = "Hight School" });
            list.Add(new education { ed_code = "ED03", ed_name = "Intermedeate" });
            list.Add(new education { ed_code = "ED03", ed_name = "Bachelor Degree" });
            list.Add(new education { ed_code = "ED05", ed_name = "Post Graduate" });

            var list2 = (from x in list
                         where (d.ed_name == null || x.ed_name.Contains(d.ed_name))
                         select x).ToList();

            return Json(new { list_data = list2 }, JsonRequestBehavior.AllowGet);
        }       

}


Sunday, January 2, 2022

Remote Validation in MVC

Remote Attributes is used in Remote Validation

1. Model : Remote Validating is used on emp_name, Rirst fill the appropriate Action Method and Error message in the Remote Attribute

public class Employee
{      
    [Key]
    public string emp_code { get; set; }
    [Remote("checkEmpName", "Home", ErrorMessage = "User name already exists")]
    public string emp_name { get; set; }
    public string emp_mobile { get; set; }
}

2. Controller : Pass  same property varibale name (emp_name) into Method checkEmpName


public class HomeController : Controller
{
    public ActionResult Create()
    {
        return View();
    }
    public JsonResult checkEmpName(string emp_name)
    {
        List<Employee> list = new List<Employee>();
        list.Add(new Employee { emp_code = "E001", emp_name = "Rinkesh", emp_mobile = "9027581975" });
        list.Add(new Employee { emp_code = "E002", emp_name = "Rakesh", emp_mobile = "9027581975" });
        list.Add(new Employee { emp_code = "E003", emp_name = "Ajay", emp_mobile = "9027581976" });

        bool st = (!list.Any(x => x.emp_name == emp_name));

        return Json(st, JsonRequestBehavior.AllowGet);
    }
}

3. View :  Required jquery is 

jquery-3.3.1.min.js, 

jquery.validate.min.js, 

jquery.validate.unobtrusive.min.js


@model Angular1Test.Models.Employee
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Create</title>
</head>
<body>
    <script src="~/Scripts/jquery-3.3.1.min.js"></script>
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="row">
            <div class="col-md-3">
                @Html.LabelFor(model => model.emp_code, htmlAttributes: new { @class = "control-label" })
                @Html.EditorFor(model => model.emp_code, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.emp_code, "", new { @class = "text-danger" })
            </div>

            <div class="col-md-3">
                @Html.LabelFor(model => model.emp_name, htmlAttributes: new { @class = "control-label" })
                @Html.EditorFor(model => model.emp_name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.emp_name, "", new { @class = "text-danger" })
            </div>
            <div class="col-md-3">
                @Html.LabelFor(model => model.emp_mobile, htmlAttributes: new { @class = "control-label" })
                @Html.EditorFor(model => model.emp_mobile, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.emp_mobile, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="row">
            <div class="col-md-12">
                <br>
                <input type="submit" value="Create" class="btn btn-default" />
                @Html.ActionLink("Back to List", "Index", new { }, new { @class = "btn btn-default" })
            </div>
        </div>
    }

    <div>
    </div>
</body>
</html>


A link to video

https://www.youtube.com/watch?v=DUej0Z8gDnA

Saturday, January 1, 2022

Edit Scaffold Templates for mvc

Edit Scaffold Templates for MVC  in Visual Studio 2019, for Create, Edit, Delete, List etc.

1. If you installer Visual Studio 2019 Enterprise Edition

C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\Extensions\Microsoft\Web\Mvc\Scaffolding\Templates\MvcView


2. If you installer Visual Studio 2019 Professional Edition 

C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\Extensions\Microsoft\Web\Mvc\Scaffolding\Templates\MvcView

Tuesday, December 28, 2021

How show console write line in asp.net in visual studio

1. We use Debug.WriteLine("sss") instead of Console.WtriteLite("aaa") 

    and it will show in Immediate Window

2. For active Immediate Window 

   Tools > Options > Debugging > General > Tick (Check) in  

    [] Redirect  All Output Window text to the Immediate Window

3. Debug uses 

using System.Diagnostics;

Friday, December 24, 2021

Stored Procedure with Sql Parameter in c#

using (SqlCommand cmd = new SqlCommand("sp_getrewardincome", con))
{
    cmd.CommandType = CommandType.StoredProcedure;
    SqlDataAdapter ad = new SqlDataAdapter(cmd);
    cmd.Parameters.AddWithValue("@id", id);
    cmd.Parameters.AddWithValue("@type", "update");
    dt = new DataTable();
    ad.Fill(dt);
   
}


using (SqlCommand cmd = new SqlCommand("sp_getrewardincome", con))
{
    cmd.CommandType = CommandType.StoredProcedure;
    SqlDataAdapter ad = new SqlDataAdapter(cmd);
    cmd.Parameters.AddWithValue("@id", id);                    
    cmd.Parameters.AddWithValue("@type", "update");
    //cmd.Parameters.Add("@result", SqlDbType.NVarChar, 3000);
    //SqlParameter ptr = cmd.Parameters["@result"];
    //ptr.Direction = ParameterDirection.Output;
    con.Open();
    cmd.ExecuteNonQuery();    
}

 

Check lock in sql server


set transaction isolation Level Repeatable read
begin transaction
select * from  [Tran]

rollback transaction

sp_lock

select OBJECT_name(949578421)
select * from sys.objects where object_id=949578421

1. If we lock


set transaction isolation Level Repeatable read
begin transaction
select * from  [Tran]

2. Checking with

sp_lock








3. Checking with object id (ObjId)

select OBJECT_name(949578421)

4. Checking with sys.Objects

select * from sys.objects where object_id=949578421 

5. To roll back locked status
rollback transaction


Create Login in MS Sql server

  https://www.youtube.com/watch?v=qfuK0V1tlrA

When web application with sql server connection using sql client, working in visual studio but not working in localhost as in iis

Error:  The EXECUTE permission was denied on the object 'StoredProcedureTest1', database 'testDb1', schema 'dbo'.  C...