日期:2014-05-18  浏览次数:20492 次

新手问题,请大家帮忙!
做了一个添加页面,总是FormatException异常,代码如下:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
  protected void Page_Load(object sender, EventArgs e)
  {

  }
  protected void btnSubmit_Click(object sender, EventArgs e)
  {
  //Pay是实体类
  Pay pay = new Pay();
  pay.name = this.lblname.Text;
  pay.basepay = float.Parse(this.lblbasepay.Text);从这开始报异常!!!!!
  pay.bonus = float.Parse(this.lblbonus.Text);
  pay.factpay = float.Parse(this.lblfactpay.Text);
  BusinessPay businesspay = new BusinessPay();
  try 
  {
  businesspay.PayInsert(pay);
  Response.Write("alert('添加成功!')");
  }
  catch(Exception se)
  {
  Response.Redirect("Error.aspx?errInfo"+se.Message);
  }
  }
}

------解决方案--------------------
int.Parse(null)则会产生异常
------解决方案--------------------
lblbasepay.Text 的值不能装化为float 另外lblbasepay.Text不会为NULL

建议使用float.TryParse
float.fValue = 0;
if(!float.TryParse(lblbasepay.Text.Trim(),out fValue ))
//show error message and break