C# 字符串和解析的正确格式
我正在尝试为我的网站执行“添加到购物车”功能,但在尝试将文本框编号转换为visual studio可以理解的值时出错 这是错误消息: () 这是我的密码 aspx.csC# 字符串和解析的正确格式,c#,html,C#,Html,我正在尝试为我的网站执行“添加到购物车”功能,但在尝试将文本框编号转换为visual studio可以理解的值时出错 这是错误消息: () 这是我的密码 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.SqlClie
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");
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用System.Data.SqlClient;
公共部分类ProductDetails:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
添加(“占位符”,“您的数量”);
}
静态只读字符串scriptStockOut=“\n”+
“警报(\“对不起,缺货!请选择较小的数量或其他产品\”);\n”+
"";
静态只读字符串scriptErrorLogin=“\n”+”警报(\“请先登录或创建帐户以方便购买\”);\n”;
受保护的无效atcbtn_单击(对象发送方,事件参数e)
{
字符串strProductId,strSQL;
int intQuantityOnHand、intBuyQuantity、newQty、intOrderNo;
十进制单位价格;
if((字符串)会话[“sFlag”]!=“T”)
{
类型csType=this.GetType();
RegisterStartupScript(csType,“Error”,scriptErrorLogin);返回;
}
SqlConnection sqlCon=new SqlConnection(@“数据源=teafamily;初始目录=BolsenF1;集成安全性=True;MultipleActiveResultSets=True;”);
sqlCon.Open();
类型csTypee=this.GetType();
sqlcmd命令;
SqlDataReader-rdr;
字符串strSQLSelect=“从产品中选择pProductID”;
sqlcmd=新的SqlCommand(strSQLSelect,sqlCon);
rdr=sqlcmd.ExecuteReader();
detailsviewrow0=DetailsView1.Rows[0];
strProductId=row0.Cells[1].Text;
strSQLSelect=“从产品中选择pQty,其中pProductID=@ProductID”;
sqlcmd=新的SqlCommand(strSQLSelect,sqlCon);
sqlcmd.Parameters.AddWithValue(“@ProductID”,strProductId);
对象oQty=sqlcmd.ExecuteScalar();
intQuantityOnHand=(int)oQty;
strSQLSelect=“从产品中选择pPrice,其中pProductID=@ProductID”;
sqlcmd=新的SqlCommand(strSQLSelect,sqlCon);
sqlcmd.Parameters.AddWithValue(“@ProductID”,strProductId);
对象oUnitPrice=sqlcmd.ExecuteScalar();
十进位价格=(十进制)盎司价格;
intBuyQuantity=int.Parse(qtyText.ToString());
新建数量=intQuantityOnHand-intBuyQuantity;
if(intQuantityOnHand
}我假设
qtyText
是一个文本框。如果是这种情况,则必须使用qtytext.Text
访问其文本/值。TextBox
的Text
属性包含其“用户提供的”//posted值
所以你应该写:
intBuyQuantity = int.Parse(qtytxt.Text);
如果需要,请不要忘记将预期的
CultureInfo
指定给相应的int.Parse()
方法重载 我不知道什么是qtyText
,但显然它是一个对象,其字符串表示形式(即qtyText.ToString()
)不是一个表示整数的字符串(错误消息显然是这么说的…),请不要发布错误消息的图片。它们不能通过搜索引擎进行搜索,这就是为什么最好将它们作为文本发布。