Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.
Showing posts with label Entity Framework Code Snippets. Show all posts
Showing posts with label Entity Framework Code Snippets. Show all posts

Thursday, September 03, 2015

Binding a Gridview using Entity Framework from SQL Server

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

public partial class BindGridView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            ContactsDataContext db = new ContactsDataContext();
            dgvContacts.DataSource = db.Contacts1;
            dgvContacts.DataBind();
        }
    }
}

Search,Update and Delete using Entity Framework to SQL Server

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

public partial class Update_EF : System.Web.UI.Page
{
    Contacts obj = new Contacts();
    ContactsDataContext db = new ContactsDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ddlID.DataSource = db.Contacts1;
            ddlID.DataBind();
            ddlID.Items.Insert(0, new ListItem("Select First Name"));
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        int ID = int.Parse(ddlID.SelectedItem.Value.ToString());
        Contacts con = db.Contacts1.SingleOrDefault(p => p.ID == ID);
        txtFirstName.Text = con.FIRSTNAME;
        txtLastName.Text = con.LASTNAME;
        txtContactNumber.Text = con.PHONENUMBER;
        lblMessage.Text = "Record Found";
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        int ID = int.Parse(ddlID.SelectedItem.Value.ToString());
        Contacts con = db.Contacts1.SingleOrDefault(p => p.ID == ID);
        con.FIRSTNAME = txtFirstName.Text;
        con.LASTNAME = txtLastName.Text;
        con.PHONENUMBER = txtContactNumber.Text;
        db.SubmitChanges();
        txtFirstName.Text = "";
        txtLastName.Text = "";
        txtContactNumber.Text = "";
        ddlID.SelectedIndex = 0;
        lblMessage.Text = "Record Updated Successfully";
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        int ID = int.Parse(ddlID.SelectedItem.Value.ToString());
        Contacts con = db.Contacts1.SingleOrDefault(p => p.ID == ID);
        db.Contacts1.DeleteOnSubmit(con);
        db.SubmitChanges();
        ddlID.SelectedIndex = 0;
        lblMessage.Text = "Record Deleted Successfully";
    }
}

Insertion into SQL Server using Entity Framework

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

public partial class Insert_EF : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        Contacts obj = new Contacts();
        obj.FIRSTNAME = txtFirstName.Text;
        obj.LASTNAME = txtLastName.Text;
        obj.PHONENUMBER = txtContactNumber.Text;
        ContactsDataContext db = new ContactsDataContext();
        db.Contacts1.InsertOnSubmit(obj);
        db.SubmitChanges();
        txtFirstName.Text = "";
        txtLastName.Text = "";
        txtContactNumber.Text = "";
        lblMessage.Text = "Record Inserted Successfully";
    }
}