Wednesday, 22 January 2014

TextboxDataAdd in Gridview Through Database Using Linq in .Net

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Collections;

public partial class _Default : System.Web.UI.Page
{
//linq class empdatacontext
//table emp

    empDataContext ee = new empDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["data"] = null;
            if (Session["data"] == null)
            {
                List<emp> emp = new List<emp>();

                GridView1.DataSource = ee.emps.ToList();
                GridView1.DataBind();

            }
            else
            {
                GridView1.DataSource = ee.emps.ToList();
                GridView1.DataBind();

            }
        }
    }
//add in grid
    protected void Button1_Click(object sender, EventArgs e)
    {
        emp em = new emp();
        var v=ee.emps.ToList();
        if (Session["data"] == null)
        {
            em.name = txtname.Text;
            em.address = txtaddress.Text;
            ee.emps.InsertOnSubmit(em);

             v = ee.emps.ToList();
            v.Add(em);
            GridView1.DataSource = v.ToList();
            GridView1.DataBind();
            Session["data"] = v;
            txtname.Text = "";
            txtaddress.Text = "";
        }
        else
        {
            List<emp> k = new List<emp>();
           
            k = (List<emp>)Session["data"];
            em.name = txtname.Text;
            em.address = txtaddress.Text;
            ee.emps.InsertOnSubmit(em);

            //k = ee.emps.ToList();
            k.Add(em);
            GridView1.DataSource = k.ToList();
            GridView1.DataBind();
            Session["data"] = k;

            txtname.Text = "";
            txtaddress.Text = "";



        }
    }
//final submit
    protected void Button2_Click(object sender, EventArgs e)
    {
       
        emp em1 = new emp();
       // var kk = k.ToList();
        var products = ee.emps.ToList();
        ee.emps.DeleteAllOnSubmit(products);
        ee.SubmitChanges();
   


//k = (List<emp>)Session["data"];
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            emp em = new emp();
            em.name = GridView1.Rows[i].Cells[0].Text.ToString();
            em.address = GridView1.Rows[i].Cells[1].Text.ToString();

            ee.emps.InsertOnSubmit(em);
            ee.SubmitChanges();
          
        }
       


        GridView1.DataSource = ee.emps.ToList();
            GridView1.DataBind();

        Session["data"] = null;
        txtname.Text = "";
        txtaddress.Text = "";


    }
}

Sunday, 12 January 2014

Learn Mvc3 (insert ,update ,delete using edmx with mvc)

http://www.pluralsight.com/training/Courses/TableOfContents/aspdotnet-mvc3-intro
http://www.w3schools.com/aspnet/mvc_intro.asp
http://malathyluxman.blogspot.in/2010/10/aspnet-mvc-viewcreate-edit-delete.html?showComment=1389597381543#c375097420805422145
http://www.c-sharpcorner.com/UploadFile/cd3310/createeditdelete-operation-on-a-table-in-Asp-Net-mvc-appli/





       First Create Edmx for connect with database
     second create controller
  ex:right click controller and add new controller  here i use employeeController

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;
namespace MvcApplication1.Controllers
{
    public class EmployeeController : Controller
    {
        //
        // GET: /Employee/



       
        CompanyEntities _db = new CompanyEntities(); //object of entity class
      
        public ActionResult Index()
        {
            return View(_db.Employees.ToList());
        }

        //
        // GET: /Employee/Details/5

        public ActionResult Details(Employee empdata)
        {
            var v = _db.Employees.ToList();
            return View(v);
        }

        //
        // GET: /Employee/reate

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Employee/Create

        [HttpPost]
        public ActionResult Create(Employee empData)
        {
            try
            {
                // TODO: Add insert logic here



                if (!ModelState.IsValid)
                    return View();
                _db.AddToEmployees(empData);
                _db.SaveChanges();
                return RedirectToAction("Index");
             
            }
            catch
            {
                return View();
            }
        }
       
        //
        // GET: /Employee/Edit/5

        public ActionResult Edit(int id)
        {
            var empToEdit = (from emp in _db.Employees where emp.ID.Equals(id) select emp).First();
            return View();
        }

        //
        // POST: /Employee/Edit/5

        [HttpPost]
        public ActionResult Edit(Employee empToEdit)
        {
            try
            {
                // TODO: Add update logic here

                var originalEmp = (from emp in _db.Employees where emp.ID.Equals(empToEdit.ID) select emp).First();
                if (!ModelState.IsValid)
                    return View(originalEmp);
                _db.ApplyPropertyChanges(originalEmp.EntityKey.EntitySetName, empToEdit);
                _db.SaveChanges();
                return RedirectToAction("Index");
              
           
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Employee/Delete/5

        public ActionResult Delete(int id)
        {
            try
            {
                // TODO: Add delete logic here

                var empToDelete = (from emp in _db.Employees where emp.ID.Equals(id) select emp).First();
                return View(empToDelete);


            }
            catch
            {
                return View();
            }
        }

        //
        // POST: /Employee/Delete/5

        [HttpPost]
        public ActionResult Delete(int id,string s)
        {
            try
          {
             // var vv = _db.Employees.Where(a => a.ID == id).ToList();
          var originalEmp = (from emp in _db.Employees where emp.ID.Equals(id) select emp).First();
           _db.DeleteObject(originalEmp);
          _db.SaveChanges();
           return RedirectToAction("Index");
          }
          catch (Exception ex)
            {
                 return View();
            }
        
        }
    }
}


then create Employee floder in view
in employee create  index,create,edit,detail,delete.aspx (ex:right click employee and use first option view)
in view second dropdown use namespace.edmx.table name select

third box select create-if create,edit-if edit,list if index




ex:Index.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcApplication1.Models.Employee>>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Index
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Index</h2>

    <table>
        <tr>
            <th></th>
            <th>
                FirstName
            </th>
            <th>
                LastName
            </th>
            <th>
                ID
            </th>
            <th>
                Designation
            </th>
        </tr>

    <% foreach (var item in Model) { %>
   
        <tr>
            <td>
                <%: Html.ActionLink("Edit", "Edit", new { id=item.ID }) %> |
                <%: Html.ActionLink("Details", "Details", new { id=item.ID })%> |
                <%: Html.ActionLink("Delete", "Delete", new { id=item.ID })%>
            </td>
            <td>
                <%: item.FirstName %>
            </td>
            <td>
                <%: item.LastName %>
            </td>
            <td>
                <%: item.ID %>
            </td>
            <td>
                <%: item.Designation %>
            </td>
        </tr>
   
    <% } %>

    </table>

    <p>
        <%: Html.ActionLink("Create New", "Create") %>
    </p>

</asp:Content>

//create.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcApplication1.Models.Employee>" %>