View Single Post
  #1 (permalink)  
Old January 22nd, 2012, 05:43 PM
leeWozyWarren leeWozyWarren is offline
Authorized User
Points: 285, Level: 5
Points: 285, Level: 5 Points: 285, Level: 5 Points: 285, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2010
Posts: 71
Thanks: 12
Thanked 0 Times in 0 Posts
Default Chapter 15 Error

Hi,

I am trying the Try it out in Ch15 with my own code. The Page_Load works fine, but the Save_Button_Click throws errors around the following:

Code:
myEntities.AddToMileages(myMileage);
Code:
.Single();
Code:
M_St_To_Miles
M_Fi_To_Miles
M_St_Fr_Miles
M_Fi_Fr_Miles
Full code behind:

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MileageModel;

public partial class Mileage : System.Web.UI.Page
{
    int _id = -1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request.QueryString.Get("MileageId")))
        {
            _id = Convert.ToInt32(Request.QueryString.Get("MileageId"));
        }
        if (!Page.IsPostBack && _id > -1)
        {
            using (MileageEntities myEntities = new MileageEntities())
            {
                var Mileage = (from r in myEntities.Mileages
                               where r.MileageId == _id
                               select r).SingleOrDefault();
                if (Mileage != null)
                {
                    WeekEnding.Text = Mileage.Weekending.ToString();

                    MondayFromHome.Text = Mileage.M_St_To_Miles.ToString();
                    MondayArriveWork.Text = Mileage.M_Fi_To_Miles.ToString();
                    MondayTotalAm.Text = Mileage.M_To_Total.ToString();
                    MondayFromWork.Text = Mileage.M_St_Fr_Miles.ToString();
                    MondayArriveHome.Text = Mileage.M_Fi_Fr_Miles.ToString();
                    MondayTotalPm.Text = Mileage.M_Fr_Total.ToString();

                    TuesdayFromHome.Text = Mileage.T_St_To_Miles.ToString();
                    TuesdayArriveWork.Text = Mileage.T_Fi_To_Miles.ToString();
                    TuesdayTotalAm.Text = Mileage.T_To_Total.ToString();
                    TuesdayFromWork.Text = Mileage.T_St_Fr_Miles.ToString();
                    TuesdayArriveHome.Text = Mileage.T_Fi_Fr_Miles.ToString();
                    TuesdayTotalPm.Text = Mileage.T_Fr_Total.ToString();

                    WednesdayFromHome.Text = Mileage.W_St_To_Miles.ToString();
                    WednesdayArriveWork.Text = Mileage.W_Fi_To_Miles.ToString();
                    WednesdayTotalAm.Text = Mileage.W_To_Total.ToString();
                    WednesdayFromWork.Text = Mileage.W_St_Fr_Miles.ToString();
                    WednesdayArriveHome.Text = Mileage.W_Fi_Fr_Miles.ToString();
                    WednesdayTotalPm.Text = Mileage.W_Fr_Total.ToString();

                    ThursdayFromHome.Text = Mileage.T_St_To_Miles.ToString();
                    ThursdayArriveWork.Text = Mileage.T_Fi_To_Miles.ToString();
                    ThursdayTotalAm.Text = Mileage.T_To_Total.ToString();
                    ThursdayFromWork.Text = Mileage.T_St_Fr_Miles.ToString();
                    ThursdayArriveHome.Text = Mileage.T_Fi_Fr_Miles.ToString();
                    ThursdayTotalPm.Text = Mileage.T_Fr_Total.ToString();

                    FridayFromHome.Text = Mileage.F_St_To_Miles.ToString();
                    FridayArriveWork.Text = Mileage.F_Fi_To_Miles.ToString();
                    FridayTotalAm.Text = Mileage.F_To_Total.ToString();
                    FridayFromWork.Text = Mileage.F_St_Fr_Miles.ToString();
                    FridayArriveHome.Text = Mileage.F_Fi_Fr_Miles.ToString();
                    FridayTotalPm.Text = Mileage.F_Fr_Total.ToString();

                    //WeeklyTotal.Text = Grand_Totals.Weekly_Total.ToString();

                    }

            }
        }

    }
    protected void SaveButton_Click(object sender, EventArgs e)
    {
        using (MileageEntities myEntities = new MileageEntities())
        {
            Mileage myMileage;
            if (_id == -1) // Insert new item
            {
                myMileage = new Mileage();

                myEntities.AddToMileages(myMileage);
            }
            else // update existing item
            {
                myMileage = (from r in myEntities.Mileages
                             where r.MileageId == _id
                             select r).Single();
            }
            myMileage.M_St_To_Miles = Convert.ToInt32(MondayFromHome.Text);
            myMileage.M_Fi_To_Miles = Convert.ToInt32(MondayArriveWork.Text);
            
            myMileage.M_St_Fr_Miles = Convert.ToInt32(MondayFromWork.Text);
            myMileage.M_Fi_Fr_Miles = Convert.ToInt32(MondayArriveHome.Text);
            
            myEntities.SaveChanges();
            Response.Redirect("Choose.aspx");
        }
    }
}
Any thoughts why?

Regards

Lee
Reply With Quote