Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 22nd, 2012, 05:43 PM
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
  #2 (permalink)  
Old January 22nd, 2012, 05:47 PM
Imar's Avatar
Wrox Author
Points: 72,035, Level: 100
Points: 72,035, Level: 100 Points: 72,035, Level: 100 Points: 72,035, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,079
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi Lee,

Does _id ever get a valid value?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #3 (permalink)  
Old January 23rd, 2012, 04:00 PM
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

Hi Imar,

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

Lee
Reply With Quote
  #4 (permalink)  
Old January 23rd, 2012, 04:04 PM
Imar's Avatar
Wrox Author
Points: 72,035, Level: 100
Points: 72,035, Level: 100 Points: 72,035, Level: 100 Points: 72,035, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,079
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #5 (permalink)  
Old January 23rd, 2012, 04:08 PM
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

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?
Reply With Quote
  #6 (permalink)  
Old January 23rd, 2012, 04:19 PM
Imar's Avatar
Wrox Author
Points: 72,035, Level: 100
Points: 72,035, Level: 100 Points: 72,035, Level: 100 Points: 72,035, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,079
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #7 (permalink)  
Old January 23rd, 2012, 04:48 PM
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

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\
Reply With Quote
  #8 (permalink)  
Old January 23rd, 2012, 05:11 PM
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

I'm not sure what these actually mean!
Reply With Quote
  #9 (permalink)  
Old January 23rd, 2012, 07:43 PM
Imar's Avatar
Wrox Author
Points: 72,035, Level: 100
Points: 72,035, Level: 100 Points: 72,035, Level: 100 Points: 72,035, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,079
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
leeWozyWarren (January 24th, 2012)
  #10 (permalink)  
Old January 24th, 2012, 02:43 PM
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

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
chapter 15: WSAT error dany96 BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 10 August 14th, 2011 03:20 PM
error while debugging in chapter 15 great.early805 BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 1 April 12th, 2008 02:27 AM
Chapter 15 Code Error - HELP! peterjstorey BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 5 January 24th, 2008 05:33 PM
Chapter 15 page 662 error lance BOOK: Beginning ASP 3.0 0 February 12th, 2004 08:56 PM
Error in simple_mailer.php ( Chapter 15 ) dwfresh Beginning PHP 3 November 26th, 2003 08:25 PM



All times are GMT -4. The time now is 12:22 AM.


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