Follow Us

LightBlog

Breaking

Thursday, June 28, 2018

insert

.cshtml




@{
    ViewBag.Title = "Index";
}
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<h2>Index</h2>
<input type="text" name="package" value="package" placeholder="Package Name"  id="txtpackage"/>
<input type="text" name="weekdayadult" value="weekdayadult" placeholder="week day adult " id="txtweekdayadult" />
<input type="text" name="weekdaychild" value="weekdaychild" placeholder="week day child" id="txtchildprice" />
<input type="text" name="weekenddayadult" value="weekenddayadultprice" placeholder="weekend day adult price" id="txtweekendadultprice" />
<input type="text" name="weekenddaychild" value="weekendaychild" placeholder="weekend day child" id="txtweekenchildprice" />
<input type="submit" name="sub" onclick="CrudOperation('Insert');" value="submit" />
<input type="submit" name="updatedata" value="update" onclick="CrudOperation('update');" />
<span id="msg"></span>
<table id="tabledata">
   
   
 
</table>
<input type="hidden" id="updateid" value="0" />
<script>
    $(document).ready(function () {
        get();
    });
    function Edit(id,packagaename,adultprice,childprice,adultwprice,childwprice)
    {
        $('#txtpackage').val(packagaename);
        $('#txtweekdayadult').val(adultprice);
        $('#txtchildprice').val(childprice);
        $('#txtweekendadultprice').val(adultwprice);
        $('#txtweekenchildprice').val(childwprice);
        $('#updateid').val(id);
    }
    function get()
    {
        $('#tabledata').empty();
        $.ajax({
            url: "@Url.Action("CrudGet","Home")",
            type: "POST",
            success: function (data) {
                debugger;
            if(data.length>0)
            {
               
                var htmltable = null;
                htmltable += " <tr>" +
                    "<th>Action</th>" +
                "<th>Package Name</th>"+
                "<th>week day adult</th>"+
                "<th>week day child</th>" +
                "<th>weekend day adult price</th>"+
                "<th>weekend day child</th>"+
                "</tr>"+
                "";
                $.each(data, function (n) {
                    htmltable += " <tr><td><input type=\"button\" value=\"Edit\" onclick=\"Edit('" + data[n].id + "','" + data[n].packagename + "','" + data[n].adultweekprice + "','" + data[n].childweekprice + "','" + data[n].adultweekendprice + "','" + data[n].childweekendprice + "');\" /></td><td></td> <td>" + data[n].packagename + "</td>" +
                    "<td>" + data[n].adultweekprice + "</td>" +
                    "<td>" + data[n].childweekprice + "</td> " +
                    "<td>" + data[n].adultweekendprice + "</td> " +
                    "<td>" + data[n].childweekendprice + "</td></tr>";

                });
               
                $('#tabledata').append(htmltable);

            }
            else {
                $('#msg').text('failed');
            }
        },
        error: function () {

        }
    });
    }
    function CrudOperation(type)
    {
        $.ajax({
            url: "@Url.Action("Crud","Home")",
            type: "POST",
            data: {
                PackageName: $('#txtpackage').val(),
                weekdayadult: $('#txtweekdayadult').val(),
                weekdaychild: $('#txtchildprice').val(),
                weekenddayadultprice: $('#txtweekendadultprice').val(),
                weekendaychild: $('#txtweekenchildprice').val(),
                id: $('#updateid').val(),
                Type: type,
            },
            dataType: "json",
            success: function (data) {
                if(data>0)
                {
                    $('#msg').text('Sucess');
                }
                else {
                    $('#msg').text('failed');
                }
                                    },
                error: function () {

                                    }
    });
    }
    function crudoperation(Type_up) {


            $.ajax({
            url: "@Url.Action("Crud_update", "Home")",
            type: "POST",
            data: {
                PackageName: $('#txtpackage').val(),
                weekdayadult: $('#txtweekdayadult').val(),
                weekdaychild: $('#txtchildprice').val(),
                weekenddayadultprice: $('#txtweekendadultprice').val(),
                weekendaychild: $('#txtweekenchildprice').val(),
                Type: Type_up,
            },
            dataType: "json",
            success: function (data) {
                if(data>0)
                {
                    $('#msg').text('Sucess');
                }
                else {
                    $('#msg').text('failed');
                }
                                    },
                error: function () {

                                    }
    });
        }
</script>
 .............................................................................................................controller...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcCrud.Models;

namespace MvcCrud.Controllers
{
    public class HomeController : Controller
    {
        Class1 cls = new Class1();
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Crud()
        {
            int result = 0;
            try
            {
                if (Request["Type"] == "Insert")
                {
                    result = cls.insert(Request["PackageName"], Convert.ToDouble(Request["weekdayadult"]), Convert.ToDouble(Request["weekdaychild"]),
                                    Convert.ToDouble(Request["weekenddayadultprice"]), Convert.ToDouble(Request["weekendaychild"]));
                    if (result == 0)
                    {
                        result = 0;
                    }
                }
                if (Request["Type"] == "update")
                {
                    result = cls.update(Convert.ToInt32( Request["id"]), Request["PackageName"], Convert.ToDouble(Request["weekdayadult"]), Convert.ToDouble(Request["weekdaychild"]),
                                    Convert.ToDouble(Request["weekenddayadultprice"]), Convert.ToDouble(Request["weekendaychild"]));
                    if (result == 0)
                        if (result == 0)
                    {
                        result = 0;
                    }
                }

            }
            catch(Exception ex)
            {
                result = 0;
            }
           
            return Json(result);
        }
        public ActionResult CrudGet()
        {
            List <PackageList> list= cls.GetList();
            return Json(list);
        }
        public ActionResult Crud_update()
        {
            int result = 0;
            try
            {
                if (Request["Type_up"] == "update")
                {
                    result = cls.update(5, Request["PackageName"], Convert.ToDouble(Request["weekdayadult"]), Convert.ToDouble(Request["weekdaychild"]),
                                    Convert.ToDouble(Request["weekenddayadultprice"]), Convert.ToDouble(Request["weekendaychild"]));
                    if (result == 0)
                    {
                        result = 0;
                    }
                }

            }
            catch (Exception ex)
            {
                result = 0;
            }

            return Json(result);
        }
    }
}


................................................................................model class..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

namespace MvcCrud.Models
{
    public class Class1
    {
        SqlConnection con1 = new SqlConnection("Data Source=asmit-PC;Initial Catalog=demo;Integrated Security=True");
        public int  insert(string packname, double weekdayadultprice, double weekdaychildp, double weekendadultp, double weekendaychildp)
        {

            con1.Open();
            SqlCommand cmd = new SqlCommand("insert into addpackage values('"+packname+"',"+weekdayadultprice+","+weekdaychildp+","+weekendadultp+","+weekendaychildp+")",con1);
           int r= cmd.ExecuteNonQuery();
            cmd.Dispose();
            con1.Close();
            return r;
        }
        public int update(int id, string packname, double weekdayadultprice, double weekdaychildp, double weekendadultp, double weekendaychildp)
        {

            con1.Open();
            SqlCommand cmd = new SqlCommand("update  addpackage set packagename='" + packname + "', weekdyadultprice=" + weekdayadultprice + ",weekdychildprice=" + weekdaychildp + ",weekendayadultprice=" + weekendadultp + ",weekendaychildprice=" + weekendaychildp + " where packageId="+id+"", con1);
            int r = cmd.ExecuteNonQuery();
            cmd.Dispose();
            con1.Close();
            return r;
        }
        public List<PackageList> GetList()
        {
            List<PackageList> list = new List<PackageList>();
            SqlDataAdapter da = new SqlDataAdapter("Select * from [dbo].[addpackage]", con1);
            DataTable dt = new DataTable();
            da.Fill(dt);
            if(dt.Rows.Count>0)
            {
                for(int i=0;i< dt.Rows.Count;i++)
                {
                    list.Add(new PackageList
                    {
                        packagename = dt.Rows[i]["packagename"].ToString(),
                        childweekprice = dt.Rows[i]["weekdychildprice"].ToString(),
                        adultweekprice = dt.Rows[i]["weekdyadultprice"].ToString(),
                        childweekendprice = dt.Rows[i]["weekendaychildprice"].ToString(),
                        adultweekendprice = dt.Rows[i]["weekendayadultprice"].ToString(),
                        id = dt.Rows[i]["packageId"].ToString(),
                    });
                }
               
            }
            return list;
        }
       
    }
    public class PackageList
    {
        public string  packagename { get; set; }
        public string id { get; set; }
        public string childweekprice { get; set; }
        public string adultweekprice { get; set; }
        public string childweekendprice { get; set; }
        public string adultweekendprice { get; set; }
     
    }
}

No comments:

Post a Comment