26 Oct 2017

How to insert data using entity framework in asp.net


How to add Entity framework in asp.net

Step.1- Add new project



Step 2-  Add new item in project:-



Step:3- Select Data option


Step:4-


Step:5- Type the server name and database name


Step:6- Click on new connection 


Step:7-


Step:8  Finesh



Aspx page Code:-

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
        <tr>
            <td>Name:</td>
            <td><asp:TextBox ID="Textname" runat="server"></asp:TextBox></td>
        </tr>

        <tr>
            <td>Gender:</td>
            <td><asp:RadioButtonList ID="rblgender" runat="server" RepeatColumns="3">
                <asp:ListItem Text="Male" Value="1"></asp:ListItem>
                <asp:ListItem Text="Female" Value="2"></asp:ListItem>
                <asp:ListItem Text="Other" Value="3"></asp:ListItem>
                </asp:RadioButtonList></td>
        </tr>

        <tr>
            <td>Country:</td>
            <td><asp:DropDownList ID="ddlcountry" runat="server"></asp:DropDownList></td>
        </tr>

        <tr>
            <td>Age:</td>
            <td><asp:TextBox ID="Textage" runat="server"></asp:TextBox></td>
        </tr>

        <tr>
            <td>Address:</td>
            <td><asp:TextBox ID="Textaddress" runat="server"></asp:TextBox></td>
        </tr>

        <tr>
            <td>Hobbies:</td>
            <td><asp:CheckBoxList ID="cblhobbies" runat="server" RepeatColumns="3">
                <asp:ListItem Text="Cricket" Value="1"></asp:ListItem>
                <asp:ListItem Text="Football" Value="2"></asp:ListItem>
                <asp:ListItem Text="Kabaddi" Value="3"></asp:ListItem>
                <asp:ListItem Text="Ballyball" Value="4"></asp:ListItem>
                <asp:ListItem Text="Music" Value="5"></asp:ListItem>
                <asp:ListItem Text="Swimming" Value="6"></asp:ListItem>
                </asp:CheckBoxList></td>
        </tr>

        <tr>
            <td></td>
            <td><asp:Button ID="btnsave" runat="server" Text="Save" OnClick="btnsave_Click" /></td>
        </tr>
    </table>
    </div>
    </form>
</body>
</html>

.CS page coding:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;


namespace Entity_framework
{
    public partial class Registration : System.Web.UI.Page
    {
        entity_framworkEntities DD = new entity_framworkEntities();
        Employee _DV = new Employee();
        Country ctr = new Country();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Fill_Country();
            }

        }
        public void Fill_Country()
        {
            var data = (from a in DD.Countries select a).ToList();
            if (data.Count > 0)
            {
                ddlcountry.DataValueField = "cid";
                ddlcountry.DataTextField = "cname";
                ddlcountry.DataSource = data;
                ddlcountry.DataBind();
                ddlcountry.Items.Insert(0, new ListItem("--Select--", "0"));
            }
        }

        protected void btnsave_Click(object sender, EventArgs e)
        {
            string HOB = "";
            for (int i = 0; i < cblhobbies.Items.Count;i++ )
            {
                if (cblhobbies.Items[i].Selected == true)
                {
                    HOB += cblhobbies.Items[i].Text + ",";
                }
            }
            HOB = HOB.TrimEnd(',');

            _DV.Name = Textname.Text;
            _DV.Gender = int.Parse(rblgender.SelectedValue);
            _DV.Country = int.Parse(ddlcountry.SelectedValue);
            _DV.Age =int.Parse(Textage.Text);
            _DV.Address = Textaddress.Text;
            _DV.Hobbies = HOB;
            DD.Employees.Add(_DV);
            DD.SaveChanges();
        }
    }
}
Read Complete

Popular Posts

Popular Posts

Translate

Total Pageviews