Wrox Programmer Forums
|
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
 
Old January 24th, 2012, 05:05 PM
Authorized User
 
Join Date: Oct 2010
Posts: 71
Thanks: 12
Thanked 0 Times in 0 Posts
Default

I tried to make some changes but was getting no where so I decided to recreate the page complete with a new table with a reduced amount of text box's. I also remade the EF .edmx file from scratch. This time I get no wriggly lines under the code showing me that something doesn't work. The new page is called Test01.aspx.

I completed the initial Page_Load code and then tested it. It worked. The text boxes took data from the database correctly with no errors. Then I added the Button_Code and that is where it fell over when I tried to browse the page.

Below is the new 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 DistanceModel;

public partial class test01 : 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 (distanceEntities myEntities = new distanceEntities())
            {
                var Mileage = (from r in myEntities.Mileages
                               where r.MileageId == _id
                               select r).SingleOrDefault();
                if (Mileage != null)
                {
                    MondayTotal.Text = Mileage.M_To_Total.ToString();
                    TuesdayTotal.Text = Mileage.T_To_Total.ToString();
                    WednesdayTotal.Text = Mileage.W_To_Total.ToString();
                    ThursdayTotal.Text = Mileage.T_To_Total.ToString();
                }
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        using (distanceEntities myEntities = new distanceEntities())
        {
            
            Mileage myMileage;

            if (_id == -1) //Insert new item
            {
                myMileage = new Mileage();

                myEntities.AddToMileages(myMileage);
            }
            else // update new item
            {
                myMileage = (from r in myEntities.Mileages
                             where r.MileageId == _id
                             select r).Single();
            }

            myMileage.M_To_Total = Convert.ToInt32(MondayTotal.Text);
            myMileage.T_To_Total = Convert.ToInt32(TuesdayTotal.Text);
            myMileage.W_To_Total = Convert.ToInt32(WednesdayTotal.Text);
            myMileage.T_To_Total = Convert.ToInt32(ThursdayTotal.Text);

            myEntities.SaveChanges();
            Response.Redirect("Choose.aspx");
        }
    }
}
This is the error the browser shows:


Code:
Compilation Error 
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: CS1502: The best overloaded method match for 'DistanceModel.distanceEntities.AddToMileages(DistanceModel.Mileage)' has some invalid arguments

Source Error:

 

Line 45:                 myMileage = new Mileage();
Line 46: 
Line 47:                 myEntities.AddToMileages(myMileage);
Line 48:             }
Line 49:             else // update new item
...
 
Old January 24th, 2012, 05:15 PM
Authorized User
 
Join Date: Oct 2010
Posts: 71
Thanks: 12
Thanked 0 Times in 0 Posts
Default

Scratch that... I deleted the old files I have set up with the errors and it all now works... so far. I shall continue testing and come back if any further problems.

Thanks for your help again Imar :)
 
Old January 26th, 2012, 06:54 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
What is the difference between development and run-time?
Development time is the stuff you do in Visual Studio. There the error list gives you compilation and syntax errors. In order to compile and run the web site, you need to resolve all these errors.

Run-time errors are errors that occur when you view the page in the browser.

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!
The Following User Says Thank You to Imar For This Useful Post:
leeWozyWarren (January 29th, 2012)
 
Old January 29th, 2012, 01:22 PM
Authorized User
 
Join Date: Oct 2010
Posts: 71
Thanks: 12
Thanked 0 Times in 0 Posts
Default

All working now - except for one thing which I wasn't expecting. I can go to my first page where I can choose a record and the query_string is passed to the next page where the EF picks it up and displays the content in my custom text_box's. I can also go direct to the page and fill in the details and when I click the save button it saves the new record. HOWEVER! If I don't complete all the fields, then it throws an error -

Here I input a number in the first text_box/field which is line 83. So it doesn't see an error for 83 and goes to 84 and doesn't find any data there!

Code:
Input string was not in a correct format. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.FormatException: Input string was not in a correct format.

Source Error: 


Line 82: 
Line 83:             myMileage.M_St_To_Miles = Convert.ToInt32(M_AM_FR_BOX.Text);
Line 84:             myMileage.M_Fi_To_Miles = Convert.ToInt32(TextBox4.Text);
Line 85:             myMileage.M_St_Fr_Miles = Convert.ToInt32(TextBox7.Text);
Line 86:             myMileage.M_Fi_Fr_Miles = Convert.ToInt32(TextBox8.Text);
 

Source File: c:\BegASPNET\mileage\test01.aspx.cs    Line: 84
I have checked the allow_nulls for the columns in the database and it is set to Yes. Here is my complete 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 DistanceModel;

public partial class test01 : 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 (distanceEntities myEntities = new distanceEntities())
            {
                var Mileage = (from r in myEntities.Mileages
                               where r.MileageId == _id
                               select r).SingleOrDefault();
                if (Mileage != null)
                {
                    WeekEndingBox.Text = Mileage.Weekending.ToString();
                    M_AM_FR_BOX.Text = Mileage.M_St_To_Miles.ToString();
                    TextBox4.Text = Mileage.M_Fi_To_Miles.ToString();
                    TextBox7.Text = Mileage.M_St_Fr_Miles.ToString();
                    TextBox8.Text = Mileage.M_Fi_Fr_Miles.ToString();
                    
                    
                    T_AM_FR_BOX.Text = Mileage.T_St_To_Miles.ToString();
                    TextBox5.Text = Mileage.T_Fi_To_Miles.ToString();
                    TextBox9.Text = Mileage.T_St_Fr_Miles.ToString();
                    TextBox10.Text = Mileage.T_Fi_Fr_Miles.ToString();
                    
                    
                    W_AM_FR_BOX.Text = Mileage.W_St_To_Miles.ToString();
                    TextBox6.Text = Mileage.W_Fi_To_Miles.ToString();
                    TextBox11.Text = Mileage.W_St_Fr_Miles.ToString();
                    TextBox12.Text = Mileage.W_Fi_Fr_Miles.ToString();
                    
                    
                    TH_AM_FR_BOX.Text = Mileage.Th_St_To_Miles.ToString();
                    TextBox20.Text = Mileage.Th_Fi_To_Miles.ToString();
                    TextBox25.Text = Mileage.Th_St_Fr_Miles.ToString();
                    TextBox26.Text = Mileage.Th_Fi_Fr_Miles.ToString();
                    
                    
                    F_AM_FR_BOX.Text = Mileage.F_St_To_Miles.ToString();
                    TextBox22.Text = Mileage.F_Fi_To_Miles.ToString();
                    TextBox27.Text = Mileage.F_St_Fr_Miles.ToString();
                    TextBox28.Text = Mileage.F_Fi_Fr_Miles.ToString();
                    
                    //WeekEndingTotalBox.Text = Mileage.Week_Total.ToString();
                }
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        using (distanceEntities myEntities = new distanceEntities())
        {
            
            Mileage myMileage;

            if (_id == -1) //Insert new item
            {
                myMileage = new Mileage();

                myEntities.AddToMileages(myMileage);
            }
            else // update new item
            {
                myMileage = (from r in myEntities.Mileages
                             where r.MileageId == _id
                             select r).Single();
            }

            myMileage.M_St_To_Miles = Convert.ToInt32(M_AM_FR_BOX.Text);
            myMileage.M_Fi_To_Miles = Convert.ToInt32(TextBox4.Text);
            myMileage.M_St_Fr_Miles = Convert.ToInt32(TextBox7.Text);
            myMileage.M_Fi_Fr_Miles = Convert.ToInt32(TextBox8.Text);
            myMileage.T_St_To_Miles = Convert.ToInt32(T_AM_FR_BOX.Text);
            myMileage.T_Fi_To_Miles = Convert.ToInt32(TextBox5.Text);
            myMileage.T_St_Fr_Miles = Convert.ToInt32(TextBox9.Text);
            myMileage.T_Fi_Fr_Miles = Convert.ToInt32(TextBox10.Text);
            myMileage.W_St_To_Miles = Convert.ToInt32(W_AM_FR_BOX.Text);
            myMileage.W_Fi_To_Miles = Convert.ToInt32(TextBox6.Text);
            myMileage.W_St_Fr_Miles = Convert.ToInt32(TextBox11.Text);
            myMileage.W_Fi_Fr_Miles = Convert.ToInt32(TextBox12.Text);
            myMileage.Th_St_To_Miles = Convert.ToInt32(TH_AM_FR_BOX.Text);
            myMileage.Th_Fi_To_Miles = Convert.ToInt32(TextBox20.Text);
            myMileage.Th_St_Fr_Miles = Convert.ToInt32(TextBox25.Text);
            myMileage.Th_Fi_Fr_Miles = Convert.ToInt32(TextBox26.Text);
            myMileage.F_St_To_Miles = Convert.ToInt32(F_AM_FR_BOX.Text);
            myMileage.F_Fi_To_Miles = Convert.ToInt32(TextBox22.Text);
            myMileage.F_St_Fr_Miles = Convert.ToInt32(TextBox27.Text);
            myMileage.F_Fi_Fr_Miles = Convert.ToInt32(TextBox28.Text);
            
            myEntities.SaveChanges();
            Response.Redirect("Choose.aspx");
        }
    }
    protected void TextBox4_TextChanged(object sender, EventArgs e)
    {

    }
}
Any thoughts?

Regards

Lee

Last edited by leeWozyWarren; January 29th, 2012 at 01:24 PM..
 
Old January 29th, 2012, 01:33 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

The database is not the issue here. The code crashes when you use Convert.ToInt32 on an empty string.

Either make this field required using a Validator, or skip the conversion to an int when the TextBox contains null or an empty string.

Hope this helps,

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!
 
Old January 29th, 2012, 01:41 PM
Authorized User
 
Join Date: Oct 2010
Posts: 71
Thanks: 12
Thanked 0 Times in 0 Posts
Default

Thanks Imar.

How would I do the skipping of the conversion if a Null was detected?

Lee
 
Old January 29th, 2012, 02:58 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You can use String.IsNullOrEmpty, like this:

Code:
if (!String.IsNullOrEmpty(yourControl.Text))
{
  yourEFObject.SomeProperty = Convert.ToInt32(yourControl.Text);
}
else
{
yourEFObject.SomeProperty = null; // if it's nullable, or some other default otherwise
}
Hope this helps,

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!
The Following User Says Thank You to Imar For This Useful Post:
leeWozyWarren (February 8th, 2012)





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.