In previous posts , i have given a example for simple registration form in , registration form in Asp.Net using 3-tier architecture. And now i am doing the same example(no changes in design) with ADO.NET Entity Data Model, and we will see how we can add Model in normal Asp.Net website (here i am not taking Asp.Net MVC project).

Using Ado.Net entity model we can reduce our coding as it becomes more easy to perform CRUD(create,update,delete) operations.

Here we will see how to add the model to our Asp.Net project and how we code for performing Create operation.
The project architecture is shown below.

Registration form Asp.Net Entity Framework

If you observe the architecture, i have added a folder Model_Files in App_Code folder and added a model EmpModel.edmx in the folder.
Before adding model, first i have designed registration form and created table in a database.
In this example, my database name is MYDB, and table name is RegisterEmp

Query to create table :

Below are the Steps to add Ado.Net Entity Data Model to our Asp.Net Application :
Step1 :
Right click on Model_Files folder, select Add New Item -> Select ADO.NET Entity Data Model -> rename it as EmpModel.edmx -> click Add
Registration form Asp.Net Entity Framework Step1
Step2: click on Next
Registration form Asp.Net Entity Framework Step2
Step 3:
Registration form Asp.Net Entity Framework Step3
Step 4: 
click on New connection , enter Server name, sql server authentication details, and click on Test Connection. click OK
Registration form Asp.Net Entity Framework Step4
Step 5:
 Registration form Asp.Net Entity Framework Step5
After testing connection, check the radio button Yes, include the sensible data in the connection string.,
check Save entity connection settings in Web.Config as, and you can change the connection string name, which will be defined in web.config when you create model.
Click on Next.
 Registration form Asp.Net Entity Framework Step5
Step 6:
 Registration form Asp.Net Entity Framework Step6
In step6, uncheck the Pluralize or singularize generated object names, select the table RegisterEmp, click on Finish.
you can see the added model, as shown in the figure below.
Registration form Asp.Net Entity Framework Step8
I have designed the registration form as shown in the figure below, and also the source code is available for download.
 Registration form Asp.Net Entity Framework
Now the code in the code behind file is like this.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Model_FILES; //including Model NameSpace
public partialclass Registration: System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
    protected void btnSubmit_Click(object sender, EventArgs e)
        MYDBEntities Entity_Obj = new MYDBEntities();
        //Employee emp = new Employee();
        RegisterEmp emp_Model_Obj = new RegisterEmp();
        string[] validFileTypes = { “doc”, “docx” };
        string ext = System.IO.Path.GetExtension(FileUploadResume.PostedFile.FileName);
        bool isValidFile = false;
            for (int i  = 0; i < validFileTypes.Length; i++)
                if (ext == “.”+ validFileTypes[i])
                    isValidFile = true;
            if (!isValidFile) //checking whether the file is valid or not
                lblMessage.ForeColor = System.Drawing.Color.Red;
                lblMessage.Text = “Invalid File. Only “ +
                string.Join(“,”, validFileTypes) + ” files are allowed..”;
            emp_Model_Obj.UserName = txtName.Text;
            emp_Model_Obj.Email = txtEmail.Text.ToLower();
            emp_Model_Obj.Password = txtPassword.Text;
            emp_Model_Obj.Gender = rbtnListGender.SelectedItem.ToString();
            emp_Model_Obj.DOB = txtDOB.Text.ToString();
            emp_Model_Obj.Mobile = txtMobile.Text;
            emp_Model_Obj.Country = txtCountry.Text;
            emp_Model_Obj.City = txtCity.Text;
            emp_Model_Obj.Address = txtAddress.Text;
            if (chkPhysical.Checked == true)
                emp_Model_Obj.Physical = “yes”;
                emp_Model_Obj.Physical = “NO”;
            if (FileUploadResume.HasFile)
                Stream fs = FileUploadResume.PostedFile.InputStream;
                emp_Model_Obj.FileName = FileUploadResume.FileName.ToString();
                BinaryReader br = new BinaryReader(fs);
                emp_Model_Obj.FileData = br.ReadBytes((Int32)fs.Length);
            //Adding Employee
            //here AddToRegisterEmp() method is added automatically by adding Model
            //saving changes to database
            lblMessage.ForeColor = System.Drawing.Color.Green;
            lblMessage.Text = “Employee Added successfully”;
        catch (Exception ex)
            lblMessage.ForeColor = System.Drawing.Color.Red;
            lblMessage.Text = ex.Message;
    protected void btnClear_Click(object sender, EventArgs e)
        //refreshing/reloading page to clear all the controls

After adding model to your website, the connection string for the entity model is defined in the web.config file. No need of manually entering the connection string.

Thanks for reading, please share your views in comment.

Download Source Code

Leave a Reply

Your email address will not be published. Required fields are marked *