Monday 8 April 2019

Linq to Sql entity framework in asp net webform part 1 | asp.net with en...







create table tbl_employee

(

emp_id int identity primary key,

emp_name nvarchar(20) not null,

emp_gender nvarchar(5) not null,

emp_addeddate datetime,

emp_modifieddate datetime



)





select * from tbl_employee

insert into tbl_employee values('salman','male',GETDATE(),null)

----------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="crud.WebForm3" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <div class="row" style="height:250px;border:1px inset gray;margin-bottom:20px;">
          <div class="container">
          <table class="table table-responsive">
              <tr>
                  <td>NAME</td>
                  <td>
                      <asp:TextBox ID="TextBox1" runat="server" CssClass="form-control"></asp:TextBox>
                  </td>
              </tr>

               <tr>
                  <td>Gneder</td>
                  <td>
                      <asp:DropDownList ID="DropDownList1" runat="server">
                          <asp:ListItem>--Select---</asp:ListItem>
                          <asp:ListItem>Male</asp:ListItem>
                          <asp:ListItem>Femle</asp:ListItem>
                      </asp:DropDownList>
                  </td>
              </tr>
              <tr>
                  <td colspan="2">
                      <asp:Button ID="Button1" runat="server" Text="Submit" CssClass="btn btn-success" OnClick="Button1_Click" />
                  </td>
              </tr>

          </table>
              </div>

    </div>



     <div class="row" style="height:750px;border:1px inset gray">

         <div class="container">
             <table class="table table-responsive " >

                 <tr>
                     <th>ID</th>
                     <th>Name</th>
                  <th>Gender</th>
                  <th>Added Date</th>
                     <th>Modified Date</th>
                 </tr>

             <asp:DataList ID="DataList1" runat="server" class="table table-responsive " >


                 <ItemTemplate>
                     <tr>
                         <td>
                             <asp:Label ID="Label1" runat="server" Text='<%# Eval("emp_id") %>'>'></asp:Label>
                         </td>
                         <td>
                             <asp:Label ID="Label2" runat="server" Text='<%# Eval("emp_name") %>'>'></asp:Label>
                         </td>
                         <td>
                             <asp:Label ID="Label3" runat="server" Text='<%# Eval("emp_gender") %>'>'></asp:Label>
                         </td>

                          <td>
                             <asp:Label ID="Label4" runat="server" Text='<%# Eval("emp_addeddate") %>'>'></asp:Label>
                         </td>
                          <td>
                             <asp:Label ID="Label5" runat="server" Text='<%# Eval("emp_modifieddate") %>'>'></asp:Label>
                         </td>

                         <td>
                             <input  type="button" value="Update" class="btn btn-info btn-sm"/>


                         </td>
                         <td>
                             <input  type="button" value="Update" class="btn btn-danger btn-sm"/>


                         </td>


                     </tr>
                   
                 </ItemTemplate>



             </asp:DataList>
             </table>




         </div>


    </div>
    </div>
    </form>
</body>
</html>
----------------------------------------------------
using crud.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace crud
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        db_aspnetEntities db = new db_aspnetEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                  DataList1 .DataSource = db.tbl_employee.OrderByDescending(x => x.emp_id).ToList();
                DataList1.DataBind();
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            tbl_employee emp = new tbl_employee();
            emp.emp_name = TextBox1.Text;
            emp.emp_gender = DropDownList1.SelectedValue.ToString();
            emp.emp_addeddate = DateTime.Now;
            db.tbl_employee.Add(emp);
            db.SaveChanges();
            DataList1.DataSource = db.tbl_employee.OrderByDescending(x => x.emp_id).ToList();
            DataList1.DataBind();
        }
    }
}




Pass Dynamically Added Html Table Records List To Controller In Asp.net MVC

Controller Code: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using ...