We provide IT Services

Register Now Click On

Read More Tutorials Click On

Post Page Advertisement [Top]

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:5- Type the server name and database name

Step:6- Click on new connection 


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">
    <form id="form1" runat="server">
            <td><asp:TextBox ID="Textname" runat="server"></asp:TextBox></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>

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

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

            <td><asp:TextBox ID="Textaddress" runat="server"></asp:TextBox></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>

            <td><asp:Button ID="btnsave" runat="server" Text="Save" OnClick="btnsave_Click" /></td>

.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)

        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.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;

No comments:

Post a Comment

| Designed by Rockprogrammer