Reputation: 369
This is homework. When I run this and click on the "Add Bike" button at the bottom of the page, the error occurs on this line: BikeBinding b = new BikeBinding { of this method:
protected void btnAddBike_Click(object sender, EventArgs e)
{
BikeBinding b = new BikeBinding {
manufacturer = Manufacturer.Text,
gears = int.Parse((Gears).Text),
frame = Frame.Text
};
Instead of int.Parse, I tried Int32.Parse for Gears, but still had the same error. Thanks in advance.
using MvcApplication3.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MvcApplication3
{
public partial class WebFormBinding : System.Web.UI.Page
{
List<BikeBinding> bicycles;
public void Page_Load(object sender, EventArgs e)
{
bicycles = GetApplicationBicycles();
FormView1.DefaultMode = FormViewMode.Edit;
BindControls();
// Page.DataBind
// FormView1.DefaultMode = FormViewMode.Edit;
}
private void BindControls()
{
// Bind all DataBound Controls
foreach (Control wc in PanelDataBoundControls.Controls)
{
if (wc is DataBoundControl)
{
DataBoundControl lc = (DataBoundControl)wc;
// Response.Write("<br />" + lc.ToString());
lc.DataSource = bicycles;
lc.DataBind();
}
}
// Not DataBound Controls, so I'll bind them by hand
DataList1.DataSource = bicycles;
DataList1.DataBind();
Repeater1.DataSource = bicycles;
Repeater1.DataBind();
}
private List<BikeBinding> GetApplicationBicycles()
{
return (List<BikeBinding>)Application["bicycles"];
}
protected void btnAddBike_Click(object sender, EventArgs e)
{
BikeBinding b = new BikeBinding {
manufacturer = Manufacturer.Text,
gears = int.Parse((Gears).Text),
frame = Frame.Text
};
bicycles.Add(b);
// Saves the bicycle list to the Application object
Application.Lock();
Application["bicycles"] = bicycles;
Application.UnLock();
Manufacturer.Text = String.Empty;
Gears.Text = String.Empty;
Frame.Text = String.Empty;
BindControls();
}
}
}
namespace MvcApplication3 {
public class MvcApplication : System.Web.HttpApplication
{
List<BikeBinding> bicycles;
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
bicycles = new List<BikeBinding>();
BikeBinding b = new BikeBinding { bikeID = 0, manufacturer = "Schwinn", gears = 10, frame = "Racing" };
bicycles.Add(b);
b = new BikeBinding { bikeID = 1, manufacturer = "Nishiki", gears = 5, frame = "Mountain" };
bicycles.Add(b);
b = new BikeBinding();
b.bikeID = 2;
b.manufacturer = "Trek Medone";
b.gears = 7;
b.frame = "Road";
for (int i = 0; i < 3; i++)
{
BikeBinding x = new BikeBinding { bikeID = i, manufacturer = i.ToString(), gears = i, frame = i.ToString() };
bicycles.Add(x);
}
// Saves the bicycle list to the Application object
Application.Lock();
Application["bicycles"] = bicycles;
Application.UnLock();
}
}
}
Upvotes: 0
Views: 1152
Reputation: 216363
If the textbox Gears
contains a value that cannot be converted to an integer the Parse
method throws an exception. So you need to change your code to handle this situation
protected void btnAddBike_Click(object sender, EventArgs e)
{
int g;
if(!Int32.TryParse(Gears.Text, out g))
{
// some label where you could write the error message
// labelError.Text = "An integer number is required.....";
return;
}
BikeBinding b = new BikeBinding {
manufacturer = Manufacturer.Text,
gears = g,
frame = Frame.Text
};
bicycles.Add(b);
.....
The TryParse method, instead of throwing an exception return false if the text cannot be converted to a integer number. If it is possible to convert then the value is assigned to the out variable passed as second parameter to TryParse
Upvotes: 3