27 Jun 2019

java script Tutorial

<!DOCTYPE html>
<html>
<head>

</head>

<body>
<h1>Introduction Of Java script Function</h1>

<p>
<span id="demo"></span><br>

<span id="demo1"></span><br>
<span id="demo2"></span><br>
<span id="demo3"></span><br>
<span id="demo4"></span><br>
<span id="demo5"></span><br>
<span id="demo6"></span><br>
<span id="demo7"></span><br>
<span id="demo8"></span><br>
<span id="demo9"></span><br>
<span id="demo10"></span><br>
<span id="demo11"></span>
</p>

<script>
var devesh = 'My name is does \'t Devesh Agrawal';

var x= "Hello";
var y= "Hello";

document.getElementById("demo1").innerHTML="My Name is :"+devesh;

document.getElementById("demo").innerHTML="My Name Length is :"+devesh.length;

document.getElementById("demo2").innerHTML="Type of devesh :"+ typeof devesh;

document.getElementById("demo3").innerHTML="Type of devesh :"+ (x===y);
</script>

</body>
</html>


<script>
<!-- var Car ={ -->
  <!-- Make:"BMW", -->
  <!-- Number:"5553" -->
<!-- }; -->
 
var newKeyword = new Object();
newKeyword.mm="Audi";
newKeyword.pp="5543i";
function constructor(make,number)
{
this.makename=make;
this.makenumber=number;
}
var car1 = new constructor("BMW","5566i");
var car2 = new constructor("BMW","5566i");
document.getElementById("demo").innerHTML="The Car Name Is: "+newKeyword.mm +"<br>"+ "The Number is :"+newKeyword.pp;

document.getElementById("demo1").innerHTML="The Car Name Is: "+ car1.makename+"<br>"+ "The Number is :"+car2.makenumber;
</script>


<script>
<!-- var Car ={ -->
  <!-- Make:"BMW", -->
  <!-- Number:"5553" -->
<!-- }; -->

var newKeyword = new Object();
newKeyword.mm="Audi";
newKeyword.pp="5543i";

function constructor(make,number)
{
this.makename=make;
this.makenumber=number;
}
var car1 = new constructor("BMW","5566i");
var car2 = new constructor("BMW","5566i");
document.getElementById("demo").innerHTML="The Car Name Is: "+newKeyword.mm +"<br>"+ "The Number is :"+newKeyword.pp;

document.getElementById("demo1").innerHTML="The Car Name Is: "+ car1.makename+"<br>"+ "The Number is :"+car2.makenumber;
</script>




<script>
var devesh = ["Manish","Dev","Varsha","Nidhi","Pooja"]



document.getElementById("demo1").innerHTML="There name is :"+ devesh.sort();

document.getElementById("demo").innerHTML="My Name Length is :"+devesh.length;

document.getElementById("demo2").innerHTML="Type of devesh :"+ typeof devesh;

document.getElementById("demo3").innerHTML="Type of devesh :"+ devesh[4];

document.getElementById("demo4").innerHTML="Type of devesh :"+ devesh.join(" and ");

document.getElementById("demo7").innerHTML="Type of value of  :"+ devesh;
devesh[0]="Dev";

var arg = [2,1,10]

arg.sort(function(a,b)
{
return (a-b);
});
document.getElementById("demo11").innerHTML="Type of value of  :"+ arg;

function Deletedata()
{
delete devesh[2];
document.getElementById("demo5").innerHTML="Type of devesh :"+ devesh;
}

function popdata()
{
devesh.pop();
document.getElementById("demo6").innerHTML="pop data of devesh :"+ devesh;
}

function pushdata()
{
devesh.push("Timmu");
document.getElementById("demo8").innerHTML="push data of devesh :"+ devesh;
}

function shiftdata()
{
devesh.shift();
document.getElementById("demo9").innerHTML="shift data of devesh :"+ devesh;
}

function unshiftdata()
{
devesh.unshift("Anshika");
document.getElementById("demo10").innerHTML="unshift data of devesh :"+ devesh;
}


</script>



<script>

var ages=[10,20,25,35,40,45,50];


function Fn(age)
{
return age>=18 && 40>=age;
}

document.getElementById("demo").innerHTML=ages.filter(Fn);
</script>


<script>
var arg=["ramesh","Poonam","Varsha","Nidhi","Divya"]

arg.forEach(Fn);

function Fn(Index,Value)
{
document.getElementById("demo").innerHTML += Index +":"+Value+"<br>";
}

</script>

Read More

23 Apr 2019

How to Bind dropdown in mvc code first approch


Controller Code:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Testmvc_12aprl.Models;
using System.Data.Entity;

namespace Testmvc_12aprl.Controllers
{
    public class HomeController : Controller
    {
        private _DbContext _db = new _DbContext();
        List<Registration> list = new List<Registration>();
        Registration regv = new Registration();
        Registrationviewmodel regvm = new Registrationviewmodel();
        List<Registrationviewmodel> tt = new List<Registrationviewmodel>();
        [HttpGet]
        public ActionResult Index(string id)
        {
            if(!string.IsNullOrEmpty(id))
            {
                int empid = Convert.ToInt16(id);
                regv = _db.registration.FirstOrDefault(x => x.Empid == empid);
                regvm.Empid = empid;
                regvm.Name = regv.Name;
                regvm.Age = regv.Age;
                regvm.Address = regv.Address;
                regvm.Phone = regv.Phone;
                regvm.Country_name = regv.Country_name;
                regvm.Email = regv.Email;
                regvm.Password = regv.Password;
            }
            ViewBag.countries = _db.country.ToList();
            List<Country> recorcountry = ViewBag.countries;
            regvm.registrationlist = _db.registration.ToList();
            List<Registration> reglist = _db.registration.ToList();
            //----------------------------------------
            var query = (from a in reglist
                         join b in recorcountry
                         on a.Country_name equals b.Cid
                         select new
                         {
                             a.Empid,
                             a.Name,
                             a.Age,
                             a.Address,
                             a.Phone,
                             a.Country_name,
                             a.Email,
                             a.Password,
                             b.Cname
                         }).ToList();


            foreach (var item in query)
            {
                Registrationviewmodel r = new Registrationviewmodel();
                r.Empid = item.Empid;
                r.Name = item.Name;
                r.Age = item.Age;
                r.Address = item.Address;
                r.Phone = item.Phone;
                r.Country_name = item.Country_name;
                r.Email = item.Email;
                r.Password = item.Password;
                r.Cname = item.Cname;
                tt.Add(r);
            }
            regvm.registrationviewmodellist = tt;
            return View(regvm);
        }
        public ActionResult Delete(string id)
        {
            int empid = Convert.ToInt16(id);
            regv = _db.registration.Find(empid);
            _db.registration.Remove(regv);
            _db.SaveChanges();
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult Insert(Registration data)
        {
            regv.Empid = data.Empid;
            regv.Name = data.Name;
            regv.Age = data.Age;
            regv.Address = data.Address;
            regv.Phone = data.Phone;
            regv.Country_name = data.Country_name;
            regv.Email = data.Email;
            regv.Password = data.Password;
            regv.Createdate = DateTime.Now;
            if (regv.Empid == 0)
            {
                _db.registration.Add(regv);
            }
            else
            {
                _db.Entry(regv).State = EntityState.Modified;         
             }
                _db.SaveChanges();
                return RedirectToAction("Index");
        }
    }
}

View Code:-

@model Testmvc_12aprl.Models.Registrationviewmodel
@{
    ViewBag.Title = "Index";
}
<br />
@using (Html.BeginForm("Insert", "Home", FormMethod.Post))
{
    @Html.HiddenFor(x => x.Empid);
    <table>
        <tr>
            <td>Name:</td>
            <td>@Html.TextBoxFor(x=>x.Name)</td>
        </tr>
        <tr>
            <td>Age:</td>
            <td>@Html.TextBoxFor(x=>x.Age)</td>
        </tr>
        <tr>
            <td>Address:</td>
            <td>@Html.TextBoxFor(x=>x.Address)</td>
        </tr>
        <tr>
            <td>Phone:</td>
            <td>@Html.TextBoxFor(x=>x.Phone)</td>
        </tr>

        <tr>
            <td>Country:</td>
            @*<td>@Html.DropDownList("Countries", new SelectList(ViewBag.countries as System.Collections.IEnumerable, "Cid", "Cname"), "--select--", new { id = "dd_country" })</td>*@
            <td>@Html.DropDownListFor(x=>x.Country_name,new SelectList(ViewBag.countries as System.Collections.IEnumerable,"Cid","Cname"),"--select--",new { id = "dd_country" })</td>
        </tr>
        <tr>
            <td>Email:</td>
            <td>@Html.TextBoxFor(x=>x.Email)</td>
        </tr>

        <tr>
            <td>Password:</td>
            <td>@Html.TextBoxFor(x=>x.Password)</td>
        </tr>
        <tr>
            <td></td>
            <td><input type="submit" id="btnsave" value="Save" /></td>
        </tr>
    </table>
}
    @if (Model != null)
    {
        @Html.Partial("_Index");
    }
    else
    {
        <div class="text-danger">Record is not Fount !!</div>
    }
<script src="~/scripts/jquery-1.10.2.min.js"></script>
<script>
    $(document).ready(function () {
        if ($("#Empid").val() != 0)
        {
            $("#btnsave").val("Update")
        }
    });
</script>

Partial View:-

@model Testmvc_12aprl.Models.Registrationviewmodel
<table class="table">
    <tr class="btn-primary alert-danger">
        <th>Name</th>
        <th>Age</th>
        <th>Address</th>
        <th>Phone</th>
        <th>Country</th>
        <th>Email</th>
        <th>Password</th>
        <th>Delete</th>
        <th>Edit</th>
    </tr>
    @foreach (var item in Model.registrationviewmodellist)
    {
        //var countryname = "";
        //foreach(var country in ViewBag.countries)
        //{
        //    if (country.Cid == item.Country_name)
        //    {
        //        countryname = country.Cname;
        //        break;
        //    }
        //}
        <tr class="btn-info">
            <td>
                @Html.DisplayFor(x => item.Name)
            </td>
            <td>
                @Html.DisplayFor(x => item.Age)
            </td>
            <td>
                @Html.DisplayFor(x => item.Address)
            </td>
            <td>
                @Html.DisplayFor(x => item.Phone)
            </td>
            <td>
                @Html.DisplayFor(x => item.Cname)
            </td>
            <td>
                @Html.DisplayFor(x => item.Email)
            </td>
            <td>
                @Html.DisplayFor(x => item.Password)
            </td>
            <td>
                @Html.ActionLink("Delete", "Delete", "Home", new { id = item.Empid },null)
            </td>
            <td>
                @Html.ActionLink("Edit", "Index", "Home", new { id = item.Empid }, null)
            </td>
        </tr>
    }
</table>
Registrationviewmodel:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Testmvc_12aprl.Models
{
    public class Registrationviewmodel
    {
        public int Empid { get; set; }
        public string Name { get; set; }
        public DateTime Age { get; set; }
        public string Address { get; set; }
        public string Phone { get; set; }
        public int Country_name { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public string Cname { get; set; }
        public string Cid { get; set; }
        public DateTime? Createdate { get; set; }
        public DateTime? Modifieddate { get; set; }
        public List<Registration> registrationlist { get; set; }
        public List<Registrationviewmodel> registrationviewmodellist { get; set; }
        public List<Country> countrylist { get; set; }
    }
}

Registration.cs:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
namespace Testmvc_12aprl.Models
{
    [Table("tableregistration")]
    public class Registration
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Empid { get; set; }
        public string Name { get; set; }
        public DateTime Age { get; set; }
        public string Address { get; set; }
        public string Phone { get; set; }
        public  int Country_name { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public DateTime? Createdate { get; set; }
        public DateTime? Modifieddate { get; set; }
    }
}

Dbcontext:-

public class _DbContext : DbContext
    {
        public _DbContext()
            : base("Defaultconnection")
        {
        }
        public DbSet<Registration> registration { get; set; }
        public DbSet<Country> country { get; set; }
    }
Read More

10 Apr 2019

Crud operation in MVC using code first approach 2019


Model  Class:- Data Model Registration:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace mvc_code_first.Models
{
   [Table("tblregistration")] 
    public class Registration
    {
        [Key]
        public int Empid { get; set; }
        public string Fname { get; set; }
        public string Lname { get; set; }
        public int Age { get; set; }
        public string Address { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public string Phone { get; set; }
        public Nullable<DateTime> create_date { get; set; }
        public Nullable<DateTime> Modified_date { get; set; }
    }
}


Step2:- Create DbContext class:-

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using AutoMapper;

namespace mvc_code_first.Models
{
    public class EmpDataContext : DbContext
    {
        public EmpDataContext()
               : base("name=Defaultconnection")
        {

        }
        public DbSet<Registration> registrations { get; set; }
        public DbSet<Header> header { get; set; }
    }
}

Step 3 View model:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace mvc_code_first.Models
{
    public class RegistrationViewModel
    {
        public int Empid { get; set; }
        public string Fname { get; set; }
        public string Lname { get; set; }
        public int Age { get; set; }
        public string Address { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public string Phone { get; set; }
        public Nullable<DateTime> create_date { get; set; }
        public List<Registration> registraionRecordList { get; set; }
    }

}


.CS page Code Controller:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using mvc_code_first.Models;
using AutoMapper;
using System.Data.Entity;
using System.Net;
using System.Net.Mail;

namespace mvc_code_first.Controllers
{
    public class HomeController : Controller
    {
        EmpDataContext db = new EmpDataContext();
        RegistrationViewModel regvm = new RegistrationViewModel();
        List<Registration> list = new List<Registration>();
        Registration reg = new Registration();

        [HttpGet]
        public ActionResult Index(string id)
        {
            if (!string.IsNullOrEmpty(id))
            {
                int empid = Convert.ToInt32(id);
                reg = db.registrations.FirstOrDefault(x => x.Empid == empid);
                regvm.Empid = empid;
                regvm.Fname = reg.Fname;
                regvm.Lname = reg.Lname;
                regvm.Age = reg.Age;
                regvm.Address = reg.Address;
                regvm.Email = reg.Email;
                regvm.Password = reg.Password;
                regvm.Phone = reg.Phone;
                regvm.create_date = reg.create_date;
            }
            List<Registration> recordList = db.registrations.ToList();
            foreach (var item in recordList)
            {
                list.Add(item);
            }
            regvm.registraionRecordList = list;
            return View(regvm);
        }

        public ActionResult Delete(string id)
        {
            int emp = Convert.ToInt16(id);
            reg = db.registrations.Find(emp);
            db.registrations.Remove(reg);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        [HttpPost]
        public ActionResult Insert(RegistrationViewModel data)
        {
            // mail start
            MailMessage mm = new MailMessage("rajatgupta3103@gmail.com","deveshagrawal1995@gmail.com");
            mm.Subject = "test";
            mm.Body = "my 1st mail program";
            mm.IsBodyHtml = false;

            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;

            NetworkCredential nc = new NetworkCredential("rajatgupta3103@gmail.com","loveyoumomdad");
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = nc;
            smtp.Send(mm);
            // mail end
            reg.Empid = data.Empid;
            reg.Fname = data.Fname;
            reg.Lname = data.Lname;
            reg.Age = data.Age;
            reg.Address = data.Address;
            reg.Email = data.Email;
            reg.Password = data.Password;
            reg.Phone = data.Phone;
            reg.create_date = reg.Empid == 0 ? DateTime.Now : data.create_date;
            if (reg.Empid == 0)
            {
                db.registrations.Add(reg);
            }
            else
            {
                reg.Modified_date = DateTime.Now;
                db.Entry(reg).State = EntityState.Modified;
            }
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        [HttpGet]
        public ActionResult HeaderTitle ()
        {
            return View();
        }

        [HttpPost]
        public ActionResult HeaderTitle(Header her)
        {
            her.create_date = DateTime.Now;
            db.header.Add(her);
            db.SaveChanges();
            return View();
        }
    }
}


View Code:-


@model mvc_code_first.Models.RegistrationViewModel
@{
    ViewBag.Title = "User Registration";
}
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<style>
    @@media print {
        .hide-in-print {
            display: none;
        }
    }
</style>
<div class="row hide-in-print">
    <div class="col-md-12">
        <div class="title_left">
            <h3>Detail</h3>
        </div>
    </div>
</div>
<div class="row hide-in-print">
    @using (Html.BeginForm("Insert", "Home", FormMethod.Post))
    {
        @Html.HiddenFor(x => x.Empid)
        @Html.HiddenFor(x => x.create_date)
        <div class="form-group">
            @Html.LabelFor(x => x.Fname, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(x => x.Fname, new { htmlAttributes = new { @class = "Form-control" } })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(x => x.Lname, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(x => x.Lname, new { htmlAttributes = new { @class = "Form-control" } })
            </div>
        </div>


        <div class="form-group">
            @Html.LabelFor(x => x.Age, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(x => x.Age, new { htmlAttributes = new { @class = "Form-control" } })
            </div>
        </div>


        <div class="form-group">
            @Html.LabelFor(x => x.Address, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(x => x.Address, new { htmlAttributes = new { @class = "Form-control" } })
            </div>
        </div>


        <div class="form-group">
            @Html.LabelFor(x => x.Email, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(x => x.Email, new { htmlAttributes = new { @class = "Form-control" } })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(x => x.Password, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(x => x.Password, new { htmlAttributes = new { @class = "Form-control" } })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(x => x.Phone, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(x => x.Phone, new { htmlAttributes = new { @class = "Form-control" } })
            </div>
        </div>

        <br />
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10" style="margin-top:10px">
                <input id="btnsave" type="submit" value="Save" class="btn btn-success" />  <input type="button" class="btn btn-success" value="Go to Admin" onclick="location.href='@Url.Action("HeaderTitle", "Home")'" />
            </div>
        </div>
    }

</div>
<br />
@{
    if (Model != null && Model.registraionRecordList.Count > 0)
    {
        @Html.Partial("_Getdata");
    }
    else
    {
        <div class="text-danger">No Records Found !!</div>
    }
    <center>
        <div>
            <button id="btnprint" class="fa fa-print hide-in-print" style="font-size:25px;color:black"></button>
        </div>
    </center>
}
<script src="~/scripts/jquery-1.10.2.min.js"></script>
<script>
    $(document).ready(function () {
        if ($("#Empid").val() != 0) {
            $("#btnsave").val("Update");
        }
    });
    $("#btnprint").click(function () {
        window.print();
    });
</script>


Patial View Code:-

@model mvc_code_first.Models.RegistrationViewModel
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<table class="table">
    <tr class="btn-primary alert-danger">
        <th>First Name</th>
        <th>Last Name</th>
        <th>Age</th>
        <th>Address</th>
        <th>Email</th>
        <th>Password</th>
        <th>Phone</th>
        <th>Delete</th>
    </tr>


    @foreach (var item in Model.registraionRecordList)
    {
        <tr class="btn-info">
            <td>
                @Html.HiddenFor(x => item.Empid)
                @if (!string.IsNullOrEmpty(item.Fname))
                {
                    @Html.ActionLink(item.Fname, "Index", "Home", new { id = item.Empid },null);
                }
            </td>
            <td>
                @Html.DisplayFor(x => item.Lname)
            </td>
            <td>
                @Html.DisplayFor(x => item.Age)
            </td>
            <td>
                @Html.DisplayFor(x => item.Address)
            </td>
            <td>
                @Html.DisplayFor(x => item.Email)
            </td>
            <td>
                @Html.DisplayFor(x => item.Password)
            </td>
            <td>
                @Html.DisplayFor(x => item.Phone)
            </td>
            <td>
                @*<i id="btndlt" class="fa fa-trash-o" style="font-size:25px;color:red"onclick="location.href='@Url.Action("Delete", "Home", new { id = item.Empid })'"></i>*@
                @Html.ActionLink("Delete", "Delete", "Home", new { id = item.Empid }, null)
            </td>
        </tr>
    }
</table>
<link href="~/scripts/sweetalert.css" rel="stylesheet" />
<script src="~/scripts/sweetalert.min.js"></script>
<script src="~/scripts/sweetalert.js"></script>

<script>
 
</script>

<script src="~/scripts/jquery-1.10.2.min.js"></script>
<script>


</script>



Read More

30 May 2018

How to insert update delete in mvc using repository pattern


How to add Controller:-


Step3:-


Step4:-How to add repository folder the create the the class in folder.


Step:6-



Controller code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using okhlainterview.Models;
using System.Data.Entity;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.UI;

namespace okhlainterview.Controllers
{
    public class HomeController : Controller
    {
        Empcontext _db = new Empcontext();
        Registration regv = new Registration();
        List<Registration> list = new List<Registration>();
        registrationviewdata regvem = new registrationviewdata();

        public IList<Registration> GetRegistrationList()
        {
            var registrionList = (_db.registration).ToList();
            return registrionList;
        }

        [HttpGet]
        public ActionResult Index(string id)
        {
            if (!string.IsNullOrEmpty(id))
            {
                int empid = Convert.ToInt16(id);
                regv = _db.registration.FirstOrDefault(x => x.Empid == empid);
                regvem.Empid = empid;
                regvem.Name = regv.Name;
                regvem.Age = regv.Age;
                regvem.Address = regv.Address;
            }

            IList<Registration> regvemlist = GetRegistrationList();
            foreach (var item in regvemlist)
            {
                list.Add(item);
                regvem.recordlist = list;
            }
            return View(regvem);
        }
        public ActionResult Delete(string id)
        {
            try
            {
                int empid = Convert.ToInt16(id);
                regv = _db.registration.Find(empid);
                _db.registration.Remove(regv);
                _db.SaveChanges();
            }
            catch (Exception ex)
            {
                throw;
            }
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult Insert(registrationviewdata data)
        {
            regv.Empid = data.Empid;
            regv.Name = data.Name;
            regv.Age = data.Age;
            regv.Address = data.Address;
            if (regv.Empid == 0)
            {
                _db.registration.Add(regv);
            }
            else
            {
                _db.Entry(regv).State = EntityState.Modified;
            }
            _db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult Exporttoexcle()
        {
            List<Registration> regvemlist = _db.registration.ToList();
            foreach (var item in regvemlist)
            {
                list.Add(item);
                regvem.recordlist = list;
            }

            var gv = new GridView();
            gv.DataSource = this.GetRegistrationList();
            gv.DataBind();

            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment; filename=DemoExcel.xls");
            Response.ContentType = "application/ms-excel";
            Response.Charset = "";
            StringWriter objStringWriter = new StringWriter();
            HtmlTextWriter objHtmlTextWriter = new HtmlTextWriter(objStringWriter);
            gv.RenderControl(objHtmlTextWriter);
            Response.Output.Write(objStringWriter.ToString());
            Response.Flush();
            Response.End();
            return View("Index");
        }
    }

}

view code:

@model okhlainterview.Models.registrationviewdata
@{
    ViewBag.Title = "Index";
}

<style>
    @@media print {
        .hide-in-print {
            display: none;
        }
    }
</style>
<div class="row hide-in-print">
    @using (Html.BeginForm("Insert", "Home", FormMethod.Post))
    {
        @Html.HiddenFor(x => x.Empid);
        <table>
            <tr>
                <td>Name:</td>
                <td>@Html.TextBoxFor(x => x.Name)</td>
            </tr>

            <tr>
                <td>Age:</td>
                <td>@Html.TextBoxFor(x => x.Age)</td>
            </tr>

            <tr>
                <td>Address:</td>
                <td>@Html.TextBoxFor(x => x.Address)</td>
            </tr>

            <tr>
                <td><input type="submit" value="Save" id="btnsave" /></td>
            </tr>
        </table>
    }
</div>

    <div>
        @if (Model != null && Model.recordlist.Count > 0)
        {
            @Html.Partial("_Index");
        }
        else
        {
            <div class="text-danger"> Record does not fount !!</div>
        }
    </div>
    <div>
        <center>
            <div>
                <button id="btnprint" value="Printa">Print</button>
            </div>
        </center>
    </div>
<a href="@Url.Action("Exporttoexcle","Home")">Exportinexcle</a>

<script src="~/scripts/jquery-1.10.2.min.js"></script>
<script>
    $(document).ready(function () {
        if ($("#Empid").val() != 0) {
            $("#btnsave").val("Update");
        }
    });
    $("#btnprint").click(function () {
        window.print();
    });
</script>

Partial view code:-
@model okhlainterview.Models.registrationviewdata

<table class="table" id="tbl">
    <tr>
        <td>Name</td>
        <td>Age</td>
        <td>Address</td>
        <td>Delete</td>
    </tr>

    @foreach (var item in Model.recordlist)
    {
        <tr>
            <td>
                @if (!string.IsNullOrEmpty(item.Name))
                {
                    @Html.ActionLink(item.Name, "Index", "Home", new { id = item.Empid }, null);
                }
            </td>

            <td>
                @Html.DisplayFor(x => item.Age)
            </td>

            <td>
                @Html.DisplayFor(x => item.Address)
            </td>
            <td>
                @Html.ActionLink("Delete", "Delete", "Home", new { id = item.Empid },null)
            </td>

            <td>
                @Html.ActionLink("Edit", "Insert", "Home", new { id = item.Empid }, null)
            </td>
        </tr>
    }

</table>

Dbcontext class:-


   public class Empcontext:DbContext
    {
        public Empcontext()
                : base("Defaultconnection")
              {

              }
       public DbSet<Registration> registration { get; set; }
    }
}
Read More

24 May 2018

How to Bind Country state and City in asp.net using jquery



Aspx Code:-


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Registration.aspx.cs" Inherits="country_state_with_jquery.Registration" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="jquery-3.2.1.min.js"></script>
    <script type="text/javascript">
        var EID = 0;
        $(document).ready(function () {
            Country_Bind();
            Data_bind();
        });

        function Country_Bind()
        {
            $.ajax({
                url: 'Registration.aspx/Fill_Country',
                type: 'post',
                contentType: 'application/json;charset=utf-8',
                dataType: 'json',
                data: "{}",
                async: false,
                success: function (_dt) {
                    _dt = JSON.parse(_dt.d);
                    //$("#ddlcountry").find("tr:gt:(0)").remove();
                    for (var i = 0; i < _dt.length; i++)
                    {
                        $("#ddlcountry").append($('<option/>').attr("value", _dt[i].Cid).text(_dt[i].Cname));
                    }
                },
                error: function ()
                {
                    alert('Country bind error !!');
                }
            });
        }

        function bindstate(Cid) {
            $.ajax({
                url: 'Registration.aspx/Fill_State',
                type: 'post',
                contentType: 'application/json;charset=utf-8',
                dataType: 'json',
                data: "{A:'" + Cid + "'}",
                async:false,
                success: function (_dt) {
                    _dt = JSON.parse(_dt.d);
                    //$("#ddlcountry").empty();
                    for (var i = 0; i < _dt.length; i++) {
                        $("#ddlstate").append($('<option/>').attr("value", _dt[i].sid).text(_dt[i].sname));
                    }
                },
                error: function () {
                    alert('State bind error  !!');
                }
            });
        }

        function bindcity(sid) {
            $.ajax({
                url: 'Registration.aspx/Fill_City',
                type: 'post',
                contentType: 'application/json;charset=utf-8',
                dataType: 'json',
                data: "{A:'" + sid + "'}",
                async: false,
                success: function (_dt) {
                    _dt = JSON.parse(_dt.d);
                    //$("#ddlcountry").empty();
                    for (var i = 0; i < _dt.length; i++) {
                        $("#ddlcity").append($('<option/>').attr("value", _dt[i].Cityid).text(_dt[i].Cityname));
                    }
                },
                error: function () {
                    alert('City bind error  !!');
                }
            });
        }

        function Savedata()
        {
            $.ajax({
                url: 'Registration.aspx/Insert',
                type: 'post',
                contentType: 'application/json;charset=utf-8',
                datatype: 'json',
                data: "{K:'" + EID + "',A:'" + $("#Textname").val() + "',B:'" + $('input:radio[Name=A]:checked').val() + "',C:'" + $("#ddlcountry").val() + "',D:'" + $("#ddlstate").val() + "',E:'" + $("#ddlcity").val() + "',F:'" + $("#Textgmail").val() + "',G:'" + $("#Textpassword").val() + "',H:'" + $("#Textcpassword").val() + "',I:'" + $("#Textage").val() + "',J:'" + $("#Textaddress").val() + "'}",
                success: function () {
                    alert('Insert data successfull !!');
                    window.location.reload(true);
                },
                error: function () {
                    alert('Insert data error !!')
                },
            });
        }

        function Data_bind() {
            $.ajax({
                url: 'Registration.aspx/Get',
                type: 'post',
                contentType: 'application/json;charset=utf-8',
                datatype: 'json',
                data: "{}",
                success: function (_dt) {
                    _dt = JSON.parse(_dt.d);
                    $("#tbl").find("tr:gt(0)").remove();
                    for (var i = 0; i < _dt.length; i++)
                    {
                        $("#tbl").append('<tr><td>' + _dt[i].name + '</td> <td>' + (_dt[i].Gender == "1" ? "Male" : _dt[i].Gender == "2" ? "Female" : "Other") + '</td> <td>' + _dt[i].Cname + '</td> <td>' + _dt[i].sname + '</td> <td>' + _dt[i].Cityname + '</td> <td>' + _dt[i].Gmail + '</td> <td>' + _dt[i].Age + '</td>  <td>' + _dt[i].Address + '</td>  <td><input type="button" id="btndelete" value="Delete" onclick="Delete_data(' + _dt[i].empid + ')" /></td>  <td><input type="button" id="btnedit" value="Edit" onclick="Edit_data(' + _dt[i].empid + ')" /></td></tr>')
                    }
                },
                error: function () {
                    alert('Data bind error  !!')
                },
            });
        }
       
        function Delete_data(empid) {
            $.ajax({
                url: 'Registration.aspx/Delete',
                type: 'post',
                contentType: 'application/json;charset=utf-8',
                datatype: 'json',
                data: "{A:'" + empid + "'}",
                success: function ()
                {
                    alert('Delete data successfull !!');
                    window.location.reload(true);
                },
                error: function ()
                {
                    alert('Delete data error !!');
                },
            });
        }

        function Edit_data(empid) {
            $.ajax({
                url: 'Registration.aspx/Edit',
                type: 'post',
                contentType: 'application/json;charset=utf-8',
                datatype: 'json',
                data: "{A:'" + empid + "'}",
                success: function (_dt) {
                    _dt = JSON.parse(_dt.d);
                    $("#Textname").val(_dt[0].name);
                    $("#ddlcountry").val(_dt[0].Country);
                    bindstate(ddlcountry.value);
                    $("#ddlstate").val(_dt[0].state);
                    bindcity(ddlstate.value);
                    $("#ddlcity").val(_dt[0].City);
                    $("#Textgmail").val(_dt[0].Gmail);
                    $("#Textpassword").val(_dt[0].password);
                    $("#Textcpassword").val(_dt[0].cpassword);
                    $("#Textage").val(_dt[0].Age);
                    $("#Textaddress").val(_dt[0].Address);
                    $("#btnsave").val("Update");
                    EID = empid;    
                },
                error: function () {
                    alert('Delete data error !!');
                },
            });
        }
        //<td>' + _dt[i].password + '</td>
        //<td>' + _dt[i].cpassword + '</td>
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>
                <tr>
                    <td>Name:</td>
                    <td><input type="text" id="Textname" /></td>
                </tr>

                <tr>
                    <td>Gender:</td>
                    <td><input type="radio" value="1" name="A" />Male
                        <input type="radio" value="2" name="A" />Female
                        <input type="radio" value="3" name="A" />Other
                    </td>
                </tr>

                <tr>
                    <td>Country:</td>
                    <td><select id="ddlcountry" onchange="bindstate(ddlcountry.value)">
                        <option value="0">--select--</option>
                        </select></td>
                </tr>


                <tr>
                    <td>State:</td>
                    <td><select id="ddlstate" onchange="bindcity(ddlstate.value)">
                        <option value="0">--select--</option>
                        </select></td>
                </tr>

                

                <tr>
                    <td>City:</td>
                    <td><select id="ddlcity">
                        <option value="0">--select--</option>
                        </select></td>
                </tr>


                <tr>
                    <td>Gmail:</td>
                    <td><input type="text" id="Textgmail" /></td>
                </tr>

                <tr>
                    <td>Password:</td>
                    <td><input type="text" id="Textpassword" /></td>
                </tr>


                <tr>
                    <td>Cpassword:</td>
                    <td><input type="text" id="Textcpassword" /></td>
                </tr>


                <tr>
                    <td>Age:</td>
                    <td><input type="text" id="Textage" /></td>
                </tr>

                <tr>
                    <td>Address:</td>
                    <td><input type="text" id="Textaddress" /></td>
                </tr>

                <tr>
                    <td></td>
                    <td><input type="button" id="btnsave" value="Save" onclick="Savedata()" /></td>
                </tr>
            </table>
            <table id="tbl" border="1" style="background-color:pink">
                <tr style="background-color:maroon;color:white">
                    <th>Name</th>
                    <th>Gender</th>
                    <th>Country</th>
                    <th>State</th>
                    <th>City</th>
                    <th>Gmail</th>
<%--                    <th>Password</th>
                    <th>CPassword</th>--%>
                    <th>Age</th>
                    <th>Address</th>
                    <th></th>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

.cs page code:-


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Services;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace country_state_with_jquery
{
    public partial class Registration : System.Web.UI.Page
    {
       static SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString);
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod]
        public static string Fill_Country()
        {
            string _dt = "";
            con.Open();
            SqlCommand cmd = new SqlCommand("usp_country_get", con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            con.Close();
            if (ds.Tables[0].Rows.Count>0)
            {
                _dt = JsonConvert.SerializeObject(ds.Tables[0]);
            }
            return _dt;
        }
        [WebMethod]
        public static string Fill_State(int A)
        {
            string _dt = "";
            con.Open();
            SqlCommand cmd = new SqlCommand("usp_state_get", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Cid", A);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            con.Close();
            if (ds.Tables[0].Rows.Count > 0)
            {
                _dt = JsonConvert.SerializeObject(ds.Tables[0]);
            }
            return _dt;
        }

        [WebMethod]
        public static string Fill_City(int A)
        {
            string _dt = "";
            con.Open();
            SqlCommand cmd = new SqlCommand("usp_state_city", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@sid", A);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            con.Close();
            if (ds.Tables[0].Rows.Count > 0)
            {
                _dt = JsonConvert.SerializeObject(ds.Tables[0]);
            }
            return _dt;
        }
        [WebMethod]
        public static void Insert(int K,string A, int B, int C, int D, int E, string F, string G, string H, int I, string J)
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("usp_employee_insrt", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Empid", K);
            cmd.Parameters.AddWithValue("@Name", A);
            cmd.Parameters.AddWithValue("@Gender", B);
            cmd.Parameters.AddWithValue("@Country", C);
            cmd.Parameters.AddWithValue("@State", D);
            cmd.Parameters.AddWithValue("@City", E);
            cmd.Parameters.AddWithValue("@Gmail", F);
            cmd.Parameters.AddWithValue("@Password", G);
            cmd.Parameters.AddWithValue("@Cpassword", H);
            cmd.Parameters.AddWithValue("@Age", I);
            cmd.Parameters.AddWithValue("@Address", J);
            cmd.ExecuteNonQuery();
            con.Close();
        }
        [WebMethod]
        public static string Get()
        {
            string _dt = "";
            con.Open();
            SqlCommand cmd = new SqlCommand("usp_employee_get", con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            con.Close();
            if (ds.Tables[0].Rows.Count > 0)
            {
                _dt = JsonConvert.SerializeObject(ds.Tables[0]);
            }
            return _dt;
        }

        [WebMethod]
        public static void Delete(int A)
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("usp_employee_delete", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Empid", A);            
            cmd.ExecuteNonQuery();
            con.Close();
        }

        [WebMethod]
        public static string Edit(int A)
        {
            string _dt = "";
            con.Open();
            SqlCommand cmd = new SqlCommand("usp_employee_edit", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Empid", A);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            con.Close();
            if (ds.Tables[0].Rows.Count > 0)
            {
                _dt = JsonConvert.SerializeObject(ds.Tables[0]);
            }
            return _dt;
        }
    }
}
Read More

Contact Form

Name

Email *

Message *

© 2014 Rock Programmer . Designed by Bloggertheme9
Powered by Rock Programmer.