p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning ASP.NET 4 : in C# and VB (http://p2p.wrox.com/forumdisplay.php?f=560)
-   -   Chapter 15 Error (http://p2p.wrox.com/showthread.php?t=86423)

leeWozyWarren January 22nd, 2012 05:43 PM

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

Imar January 22nd, 2012 05:47 PM

Hi Lee,

Does _id ever get a valid value?

Imar

leeWozyWarren January 23rd, 2012 04:00 PM

Hi Imar,

I thought that _id was a variable! I have no actual data using _id in a data or view.

Lee

Imar January 23rd, 2012 04:04 PM

It is a variable, which should get a value from the query string. Does it successfully get a value?

Imar

leeWozyWarren January 23rd, 2012 04:08 PM

Hi Imar,

_id in the page_load gets a value from a query string when a user clicks a link in an other page.

But the _id in the button_click doesn't get a value for "insert a new item" but it should be able to take a value for an update from the query string. Or doesn't it?

Imar January 23rd, 2012 04:19 PM

Yes. Judging from your description, Single() returns null, which seems to imply the item you're editing does not exist in the database.

Did you try debugging your code to see where it breaks exactly?

Cheers,

Imar

leeWozyWarren January 23rd, 2012 04:48 PM

These are the errors that the debugging throws up...


Code:

Error        1        'MileageModel.MileageEntities' does not contain a definition for 'AddToMileage' and no extension method 'AddToMileage' accepting a first argument of type 'MileageModel.MileageEntities' could be found (are you missing a using directive or an assembly reference?)        C:\BegASPNET\mileage\Mileage.aspx.cs        82        28        C:\BegASPNET\mileage\
Code:

Error        2        Cannot implicitly convert type 'MileageModel.Mileage' to 'Mileage'        C:\BegASPNET\mileage\Mileage.aspx.cs        88        39        C:\BegASPNET\mileage\
Error        3        'Mileage' does not contain a definition for 'M_St_To_Miles' and no extension method 'M_St_To_Miles' accepting a first argument of type 'Mileage' could be found (are you missing a using directive or an assembly reference?)        C:\BegASPNET\mileage\Mileage.aspx.cs        90        23        C:\BegASPNET\mileage\

Code:

Error        4        'Mileage' does not contain a definition for 'M_Fi_To_Miles' and no extension method 'M_Fi_To_Miles' accepting a first argument of type 'Mileage' could be found (are you missing a using directive or an assembly reference?)        C:\BegASPNET\mileage\Mileage.aspx.cs        91        23        C:\BegASPNET\mileage\
Code:

Error        5        'Mileage' does not contain a definition for 'M_St_Fr_Miles' and no extension method 'M_St_Fr_Miles' accepting a first argument of type 'Mileage' could be found (are you missing a using directive or an assembly reference?)        C:\BegASPNET\mileage\Mileage.aspx.cs        93        23        C:\BegASPNET\mileage\
Code:

Error        6        'Mileage' does not contain a definition for 'M_Fi_Fr_Miles' and no extension method 'M_Fi_Fr_Miles' accepting a first argument of type 'Mileage' could be found (are you missing a using directive or an assembly reference?)        C:\BegASPNET\mileage\Mileage.aspx.cs        94        23        C:\BegASPNET\mileage\

leeWozyWarren January 23rd, 2012 05:11 PM

I'm not sure what these actually mean!

Imar January 23rd, 2012 07:43 PM

You get these at development time, not at run-time right? That means something is wrong.... ;-)

I can see a few possible reasons for this problem:

1. Your ASPX page is called Mileage as well. This may or may not cause a name conflict with your mileage class. Try create a new file with a new name and copy in the code (or rename the file, and the reference to Mileage in the markup and code behind.)

2.Something is wrong with your model so it doesn't compile. Try commenting out the failing code in this ASPX page, cose all open files, and press Ctrl+Shift+B to rebuild all code. Do you still get errors?

Imar

leeWozyWarren January 24th, 2012 02:43 PM

Thanks Imar. I will try 1 and 2 and see what happens. What is the difference between development and run-time?


All times are GMT -4. The time now is 01:55 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.