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

求帮忙:输入字符串的格式不正确。
public partial class shoppingCart : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
  if (!Page.IsPostBack)
  {
  bindData();
  }
  }
  private void bindData()
  {
  gdvCart.DataSource = (DataTable)Session["shoppingCart"];
  gdvCart.DataBind();
  }
  protected void gdvCart_RowCommand(object sender, GridViewCommandEventArgs e)
  {
  //if (e.CommandName == "delete")
  //{
  // int index = Convert.ToInt32(e.CommandArgument);
  // //GridViewRow row = GridView1.Rows[index];
  // //int bid = Convert.ToInt32(row.Cells[2].Text);

  // DataTable dt = (DataTable)Session["shoppingCart"];
  // //delete one row
  // dt.Rows.RemoveAt(index);

  // //save again
  // Session["shoppingCart"] = dt;
  // bindData();
  //}

  DataTable cart = new DataTable();

  if (Session["shoppingCart2"] == null)
  {
  cart.Columns.Add("bookID", typeof(int));
  cart.Columns.Add("bookName", typeof(string));
  cart.Columns.Add("quantity", typeof(int));
  cart.Columns.Add("price", typeof(double));
  cart.Columns.Add("buyTime", typeof(DateTime));
  Session["shoppingCart2"] = cart;
  }

  cart = (DataTable)Session["shoppingCart2"];

  int index = Convert.ToInt32(e.CommandArgument);
  GridViewRow row = gdvCart.Rows[index];
  DataRow rr = cart.NewRow();

  rr["bookID"] = Convert.ToInt32(row.Cells[2].Text);
  rr["bookName"] = Convert.ToString(row.Cells[3].Text);
  TextBox tt = (TextBox)row.Cells[1].FindControl("txtQuan");
  rr["quantity"] = Convert.ToInt32(tt.Text);
  rr["price"] = Convert.ToDouble(row.Cells[4].Text);
rr["buyTime"] = Convert.ToDateTime(row.Cells[5].Text);

  cart.Rows.Add(rr);
  Session["shoppingCart2"] = cart;
  }
  protected void btnBuy_Click(object sender, EventArgs e)
  {
  //invalid customer
  if (Session["userID"] == null)
  {
  Response.Write("<script>window.alert('抱歉,只有登录系统才可以结账!')</script>");
  Session["shoppingCart"] = null;
  }
  //valid customer
  else
  {
  Response.Redirect("buyCenter.aspx");
  }
  }
}


------解决方案--------------------
row.Cells[4].Text的文本无法转换成double,确认它是一个合法的数字。
------解决方案--------------------
按照你的数据定义,Cells[3]才是price值啊,因为Cells是从0开始的索引
------解决方案--------------------
对,Cells[3]才对,报这个错表示源文本不是数字格式的字符串。