我正在嘗試編寫具有3個文本框的代碼,並且需要將輸入到框中的信息傳遞給SQL數據庫,然後將其顯示在一個gridview,用作購物車。我一直在我的catch塊中發現一個錯誤,那就是說我必須定義@strItemName的標量變量。這是我的代碼...任何幫助將非常讚賞。如果這段代碼在網站上的格式不正確,這是我的第一篇文章。提前致謝。必須在SQL INSERT語句中定義縮放變量
protected void btnSubmit_Click(object sender, EventArgs e)
{
string strItemName = txtTitle.Text;
string strMovieQuantity = txtNumMovies.Text;
string strMoviePrice = txtPrice.Text;
//string strCurrentUser = HttpContext.Current.User.Identity.Name;
// I need this later to have a current user also inserted into the table
string strConnection = ConfigurationManager.ConnectionStrings["cs3200"].ToString();
SqlConnection myConnection = new SqlConnection(strConnection);
string strSql = "INSERT INTO ShoppingCart ([ItemName], [QuantityOrdered], [UnitPrice]) VALUES (@strItemName, @strQuantityOrdered, @strUnitPrice)";
SqlCommand myCommand = new SqlCommand(strSql, myConnection);
try
{
myConnection.Open();
int intCnt = myCommand.ExecuteNonQuery();
if (intCnt > 1)
{
lblInsert.Text = intCnt.ToString() + "Records was added";
}
else
{
lblInsert.Text = "One Record was inserted";
}
myConnection.Close();
}
catch (Exception ex)
{
lblInsert.Text = "Unable to insert your record" + "</br>";
lblInsert.Text += "Error Message = " + ex.Message;
myConnection.Close();
}
cmd.Parameters.Add()...完全缺失... –