2010-11-27 20 views
1

次のチュートリアルを参照:私はWEBアプリケーションを使用して同じコードを作成しようとしていますADO.NET Entity Framework Tutorial and BasicsASP.NET Entity Frameworkの更新データ

。情報を取得することはできますが、更新ボタンイベントは変更を保存しません。 CurrentPayrollオブジェクトは、何らかの理由で更新ボタンが押されたときにnullです。 CurrentPayrollオブジェクトを設定する別のAuthorsを選択します。私はセッションを使ってみましたが、それもうまくいきません。ここで

は、コードは次のとおりです。

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 != 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) 
    { 

    } 
} 

}

答えて

1

。 Payrollオブジェクトをデタッチする必要があります。新しいコンテキストを作成し、保存したオブジェクトを新しいコンテキストにアタッチします。

publishContext.Detach(CurrentPayroll);ここで

が更新されたイベントです:

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(); 
    } 
関連する問題