Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3440592
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:27:01+00:00 2026-05-18T08:27:01+00:00

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

  • 0

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 != 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:27:01+00:00Added an answer on May 18, 2026 at 8:27 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.