 |
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

January 22nd, 2012, 05:43 PM
|
Authorized User
|
|
Join Date: Oct 2010
Posts: 71
Thanks: 12
Thanked 0 Times in 0 Posts
|
|
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:
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
|

January 22nd, 2012, 05:47 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi Lee,
Does _id ever get a valid value?
Imar
|

January 23rd, 2012, 04:00 PM
|
Authorized User
|
|
Join Date: Oct 2010
Posts: 71
Thanks: 12
Thanked 0 Times in 0 Posts
|
|
Hi Imar,
I thought that _id was a variable! I have no actual data using _id in a data or view.
Lee
|

January 23rd, 2012, 04:04 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
It is a variable, which should get a value from the query string. Does it successfully get a value?
Imar
|

January 23rd, 2012, 04:08 PM
|
Authorized User
|
|
Join Date: Oct 2010
Posts: 71
Thanks: 12
Thanked 0 Times in 0 Posts
|
|
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?
|

January 23rd, 2012, 04:19 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|

January 23rd, 2012, 04:48 PM
|
Authorized User
|
|
Join Date: Oct 2010
Posts: 71
Thanks: 12
Thanked 0 Times in 0 Posts
|
|
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\
|

January 23rd, 2012, 05:11 PM
|
Authorized User
|
|
Join Date: Oct 2010
Posts: 71
Thanks: 12
Thanked 0 Times in 0 Posts
|
|
I'm not sure what these actually mean!
|

January 23rd, 2012, 07:43 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
The Following User Says Thank You to Imar For This Useful Post:
|
|

January 24th, 2012, 02:43 PM
|
Authorized User
|
|
Join Date: Oct 2010
Posts: 71
Thanks: 12
Thanked 0 Times in 0 Posts
|
|
Thanks Imar. I will try 1 and 2 and see what happens. What is the difference between development and run-time?
|
|
 |
|