开发者

ASP.NET Entity Framework Update Data

Referring to the following tutorial: ADO.NET Entity Framework Tutorial and Basics

I am trying to create the same code using a WEB application. I am able to fetch information, however my update button event doesn't save the changes. The CurrentPayroll object is null for some reason when the update button is pressed. I do select different Authors which sets the CurrentPayroll object. I tried using sessions, but that also does not work.

Here is the code:

PayrollView.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
CodeBehind="PayrollView.aspx.cs" Inherits="SodiumHydroxide.Public.PayrollView" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
<style type="text/css">
    .style1
    {
        width: 100%;
    }
    .style2
    {
    }
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<table class="style1">
    <tr>
        <td class="style2">
            Author
        </td>
        <td>
            <asp:DropDownList ID="ddAuthor" runat="server" OnSelectedIndexChanged="ddAuthor_SelectedIndexChanged"
                AutoPostBack="true" ViewStateMode="Enabled">
            </asp:DropDownList>
        </td>
    </tr>
    <tr>
        <td class="style2">
            PayrollID
        </td>
        <td>
            <asp:Label ID="lblPayRollID" runat="server" Text="000"></asp:Label>
        </td>
    </tr>
    <tr>
        <td class="style2">
            Salary
        </td>
        <td>
            <asp:TextBox ID="txtSalary" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td class="style2" colspan="2">
            <asp:Button ID="btnFirst" runat="server" Text="&lt;&lt;" />
            <asp:Button ID="btnPrevious" runat="server" Text="&lt;" />
            <asp:Button ID="btnNext" runat="server" Text="&gt;" />
            <asp:Button ID="btnLast" runat="server" Text="&gt;&gt;" />
        </td>
    </tr>
    <tr>
        <td class="style2" colspan="2">
            <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
            <asp:Button ID="btnUpdate" runat="server" Text="Update" OnClick="btnUpdate_Click" />
            <asp:Button ID="btnDelete" runat="server" Text="Delete" OnClick="btnDelete_Click" />
            <asp:Label ID="lblFeedback" runat="server" ForeColor="Red"></asp:Label>
        </td>
    </tr>
</table>
</asp:Content>

PayrollView.aspx.cs

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

namespace SodiumHydroxide.Public
{
    public partial class PayrollView : System.Web.UI.Page
    {
    PublishingCompanyEntities publishContext;
    Payroll CurrentPayroll;

    protected void Page_Load(object sender, EventArgs e)
    {
        publishContext = new PublishingCompanyEntities();

        if (!Page.IsPostBack)
        {
            try
            {
                this.ddAuthor.DataSource = publishContext.Author;
                this.ddAuthor.DataTextField = "FirstName";
                this.ddAuthor.DataValueField = "AuthorID";
                this.ddAuthor.DataBind();
            }
            catch (ObjectDisposedException)
            {
            }
        } // if (!Page.IsPostBack)
    }

    protected void ddAuthor_SelectedIndexChanged(object sender, EventArgs e)
    {
        int Selected = 0;

        try
        {
            Selected = Convert.ToInt32(this.ddAuthor.SelectedItem.Value);
        }
        catch (InvalidCastException) { }

        if (Selected > 0)
        {
            Author Authors = new Author();
            Authors.AuthorID = Selected;

            //Uses Linq-to-Entities
            IQueryable<Payroll> payrollQuery =
               from p in publishContext.Payroll
               where p.Author.AuthorID == Authors.AuthorID
               select p;
            List<Payroll> SelectedPayroll = payrollQuery.ToList();

            if (SelectedPayroll != 开发者_JS百科null && SelectedPayroll.Count > 0)
            {
                CurrentPayroll = SelectedPayroll.First();

                Session["CurrentPayroll"] = CurrentPayroll;
            }
            else
            {
                CurrentPayroll = null;

                Session["CurrentPayroll"] = CurrentPayroll;
            }
        }

        PopulateFields();

        this.lblFeedback.Text = "ddAuthor_SelectedIndexChanged " + Selected.ToString();
    }

    private void PopulateFields()
    {
        if (CurrentPayroll != null)
        {
            this.lblPayRollID.Text = CurrentPayroll.PayrollID.ToString();
            this.txtSalary.Text = CurrentPayroll.Salary.ToString();
            this.btnAdd.Enabled = false;
            this.btnDelete.Enabled = true;
            this.btnUpdate.Enabled = true;
        }
        else
        {
            this.lblPayRollID.Text = "Not on payroll";
            this.txtSalary.Text = "0";
            this.btnAdd.Enabled = true;
            this.btnDelete.Enabled = false;
            this.btnUpdate.Enabled = false;
        }
    }

    protected override void OnUnload(EventArgs e)
    {
        base.OnUnload(e);
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {

    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        // Payroll UpdatePayroll = (Payroll)Session["CurrentPayroll"];

        CurrentPayroll.Salary = Convert.ToInt32(this.txtSalary.Text);

        publishContext.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {

    }
}

}


Managed to sort it out. You need to detach the Payroll object. Create a new context, then attach the stored object to the new context.

publishContext.Detach(CurrentPayroll);

Here is the updated event:

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        Payroll StoredPayroll = (Payroll)Session["CurrentPayroll"];

        PublishingCompanyEntities UpdateContext = new PublishingCompanyEntities();
        UpdateContext.Attach(StoredPayroll);

        StoredPayroll.Salary = Convert.ToInt32(this.txtSalary.Text);

        int AffectedRows = UpdateContext.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);

        this.lblFeedback.Text = "Affected Rows: " + AffectedRows.ToString();
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜