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.

Thursday, September 03, 2015

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";
    }
}

No comments:

Post a Comment