-5

在上一页我有

protected void SqlCheckout_Inserted(object sender, SqlDataSourceStatusEventArgs e)
{
    string CustID;

    if (e.Exception == null)
    {
        CustID = e.Command.Parameters["@CustomerID"].Value.ToString();
        Response.Redirect("Payment.aspx?id=" + CustID);
    }
}

然后在我的付款页面

protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
    int intCustID;
    int intOrderID;

    intCustID = int.Parse(Request.QueryString["CustomerID"]);

    //save shopping cart
    ShoppingCart objCart;

    //retreive shoppping cart from session
    objCart = (ShoppingCart)Session["shoppingCart"];
    //the shopping cart cannot be empty
    if (objCart != null)
    {
        //save Cart
        intOrderID = objCart.SaveCart(intCustID);
        e.Values["OrderID"] = intOrderID;
        Session["OrderID"] = intOrderID;

    }
    else
    {
        e.Cancel = true;
    }
}    

我遵循教程编辑:这允许我将数据插入数据库,并且由于某种原因在这行代码中我收到错误消息说输入字符串格式不正确编辑:并且“值不能为空”...有小费吗?

4

4 回答 4

3

查询字符串参数id不是整数。

如果您不确定参数的有效性,请改用 TryParse。

int intCustId;
if(int.TryParse(Request.QueryString["id"], out intCustId)
{
  // Do stuff
}
else
{
  // Handle error
}
于 2013-06-05T15:06:00.203 回答
1

Based on your comment:

Im getting an error saying Input string was not in correct format EDIT: and The ' value cannot be null' ... any tips?

It sounds like when you arrive on this page you do not have id in the query string. So most likely your url looks like this:

http://mysite.com/mypage.aspx

and it needs to look like this:

http://mysite.com/mypage.aspx?id=1234

In order to fix this you will most likely need to go to the previous page (the page that you navigate TO the page with the error FROM) and figure out why id isn't getting passed in the query string.

UPDATE

Your original code suggested you were trying to pull:

Request.QueryString["id"]

While your update suggests:

Request.QueryString["CustomerID"]

Based on your comment below neither of these are correct. You need to match the query string exactly (including case). Try the code below:

Request.QueryString["ID"]

As other's have mentioned you should probably be using TryParse also.

于 2013-06-05T15:24:32.647 回答
1

首先,确保Request.QueryString["id"]不为 null 或为空。

int intCustID  = string.IsNullOrEmpty(Request.QueryString["id"]) ? 0 : int.Parse(Request.QueryString["id"]);

这将:

  • 检查您的Request.QueryString["id"]是否为空或为空。
  • 如果是,它将返回零,否则,它将把字符串解析为int
  • 将解析int的值分配给intCustID

您还可以使用:

int intCustID  = Convert.ToInt32(Request.QueryString["id"]);
于 2013-06-05T15:05:58.687 回答
0

这意味着Request.QueryString["id"]返回的字符串不是整数。

你得到的可能是

  • 空,以便您尝试转换空字符串
  • 一个非数字字符串,显然不能转换为整数
  • 十进制格式数字,例如“5.0”,而不是“5”,这也会给出此异常。在这种情况下,您可以double.Parse改用,然后转换为 int 。
于 2013-06-05T15:04:23.840 回答