View Single Post
  #14 (permalink)  
Old January 29th, 2012, 01:22 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

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..
Reply With Quote