字符串和解析的正确格式

时间:2018-07-08 17:37:00

标签: c# html

我正在尝试为我的网站添加购物车功能,但是在尝试将文本框数字转换为Visual Studio可以理解的值时遇到错误。

这是错误消息:

Link to full-size image

这是我的代码

aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class ProductDetails : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
    qtytxt.Attributes.Add("placeholder", "Your Quantity");
}
static readonly string scriptStockOut = "<script language=\"javascript\">\n" +
"alert (\"Sorry Stock Out!  Please choose a smaller quantity or another product \");\n" +
"</script>";

static readonly string scriptErrorLogin = "<script language=\"javascript\">\n" + "alert (\"Please login or create account first to facilitate buying\");\n</script>";



protected void atcbtn_Click(object sender, EventArgs e)
{
    string strProductId, strSQL;
    int intQuantityOnHand, intBuyQuantity, newQty, intOrderNo;
    decimal decUnitPrice;

    if ((string)Session["sFlag"] != "T")
    {
        Type csType = this.GetType();
        ClientScript.RegisterStartupScript(csType, "Error", scriptErrorLogin); return;
    }

    SqlConnection sqlCon = new SqlConnection(@"Data Source=teafamily;Initial Catalog=BolsenF1;Integrated Security=True;MultipleActiveResultSets=true;");
    sqlCon.Open();
    Type csTypee = this.GetType();
    SqlCommand sqlcmd;
    SqlDataReader rdr;
    string strSQLSelect = "SELECT pProductID FROM Products";
    sqlcmd = new SqlCommand(strSQLSelect, sqlCon);
    rdr = sqlcmd.ExecuteReader();

    DetailsViewRow row0 = DetailsView1.Rows[0];
    strProductId = row0.Cells[1].Text;

    strSQLSelect = "SELECT pQty FROM Products WHERE pProductID=@ProductID";
    sqlcmd = new SqlCommand(strSQLSelect, sqlCon);
    sqlcmd.Parameters.AddWithValue("@ProductID", strProductId);
    object oQty = sqlcmd.ExecuteScalar();
    intQuantityOnHand = (int)oQty;

    strSQLSelect = "SELECT pPrice FROM Products WHERE pProductID=@ProductID";
    sqlcmd = new SqlCommand(strSQLSelect, sqlCon);
    sqlcmd.Parameters.AddWithValue("@ProductID", strProductId);
    object oUnitPrice = sqlcmd.ExecuteScalar();
    decUnitPrice = (decimal)oUnitPrice;

    intBuyQuantity = int.Parse(qtytxt.ToString());

    newQty = intQuantityOnHand - intBuyQuantity;

    if (intQuantityOnHand < intBuyQuantity)
    {
        Type csType = this.GetType();
        ClientScript.RegisterStartupScript(csType, "StockOut", scriptStockOut);
    }

    Session["sProductId"] = strProductId;
    Session["sUnitPrice"] = decUnitPrice.ToString();
    Session["sQuantity"] = newQty.ToString();

    intOrderNo = (int)Session["sOrderNo"];
    strSQL = "INSERT INTO orderItems(iOrderNo,iProductID, iQty, iUnitPrice)"
        + "VALUES (@OrderNo, @ProductID, @Qty, @UnitPrice)";
    sqlcmd = new SqlCommand(strSQL, sqlCon);
    sqlcmd.Parameters.AddWithValue("@OrderNo", intOrderNo);
    sqlcmd.Parameters.AddWithValue("@ProductID", strProductId);
    sqlcmd.Parameters.AddWithValue("@Qty", intBuyQuantity);
    sqlcmd.Parameters.AddWithValue("@UnitPrice", decUnitPrice);
    sqlcmd.ExecuteNonQuery();

    strSQL = "UPDATE Products SET pQty=@NewQty WHERE pProductID = @ProductID";
    sqlcmd = new SqlCommand(strSQL, sqlCon);
    sqlcmd.Parameters.AddWithValue("@NewQty", newQty);
    sqlcmd.Parameters.AddWithValue("@ProductID", strProductId);
    sqlcmd.ExecuteNonQuery();

    sqlCon.Close();

    Response.Redirect("ShoppingCart.aspx");

}

}

1 个答案:

答案 0 :(得分:0)

我假设qtytxt是一个TextBox。在这种情况下,必须使用qtytxt.Text来访问其文本/值。 Text的{​​{1}}属性包含其“用户提供” /发布的值。

所以你应该写:

TextBox

如果必要,请不要忘记为适当的intBuyQuantity = int.Parse(qtytxt.Text); 方法重载指定期望的CultureInfo