我正在创建一个网上商店,我在添加产品方面遇到问题而且我总是收到致命的错误。我尝试了不同类型的方法只是为了摆脱我的问题,我仍然无法弄明白。希望得到一些帮助和解释。谢谢。
protected void btn_submit_Click(object sender, EventArgs e)
{
constructor var = new constructor();
var.ItemName = txtbx_proName.Text;
if (rd_car.Checked)
{
var.ItemID = '1';
}
else if (rd_acc.Checked)
{
var.ItemID = '2';
}
else if (rd_others.Checked)
{
var.ItemID = '3';
}
var.Price = Convert.ToDouble(txtbx_price.Text);
var.Stock = Convert.ToInt32(txtbx_stock.Text);
var.Description = txtbx_desc.Text;
//var.Image = image;
using (MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["DBCon"].ConnectionString))
{
try
{
con.Open();
string sql = "INSERT INTO product_tbl (`id`, `product_name`, `product_price`, `product_desc`, `product_type`, `product_stock`) VALUES (NULL,@itemname,@pro_price,@pro_desc,@pro_stock);";
MySqlCommand cmd = new MySqlCommand(sql, con);
cmd.Parameters.AddWithValue("@itemname", var.ItemName);
cmd.Parameters.AddWithValue("@pro_desc", var.Description);
cmd.Parameters.AddWithValue("@pro_price", var.Price);
cmd.Parameters.AddWithValue("@pro_type", var.ItemID);
cmd.ExecuteNonQuery();
lbl_result.Text = "New Product Added!";
txtbx_desc.Text = "";
txtbx_price.Text = "";
txtbx_proName.Text = "";
txtbx_stock.Text = "";
}
catch (Exception ex)
{
lbl_result.Text = "ERROR >> " + ex.Message + "!";
txtbx_desc.Text = "";
txtbx_price.Text = "";
txtbx_proName.Text = "";
txtbx_stock.Text = "";
}
finally
{
con.Close();
}
}
}
构造函数代码:
public class constructor
{
private string _uname, _pword, _conNum, _desc, _itemName, _email, _fName, _lName, _address, _categ, _changePword, _answer, _image;
private int _uid, _stock, _itemId;
[![enter image description here][1]][1]private double _price;
public string Image
{
get { return _image; }
set { _image = value; }
}
public string Answer
{
get { return _answer; }
set { _answer = value; }
}
public string ChangePass
{
get { return _changePword; }
set { _changePword = value; }
}
public string Category
{
get { return _categ; }
set { _categ = value; }
}
public string Username
{
get { return _uname; }
set { _uname = value; }
}
public string Password
{
get { return _pword; }
set { _pword = value; }
}
public string Description
{
get { return _desc; }
set { _desc = value; }
}
public string ItemName
{
get { return _itemName; }
set { _itemName = value; }
}
public string Email
{
get { return _email; }
set { _email = value; }
}
public string FirstName
{
get { return _fName; }
set { _fName = value; }
}
public string LastName
{
get { return _lName; }
set { _lName = value; }
}
public string Address
{
get { return _address; }
set { _address = value; }
}
public double Price
{
get { return _price; }
set { _price = value; }
}
public int UserId
{
get { return _uid; }
set { _uid = value; }
}
public int Stock
{
get { return _stock; }
set { _stock = value; }
}
public string ContactNumber
{
get { return _conNum; }
set { _conNum = value; }
}
public int ItemID
{
get { return _itemId; }
set { _itemId = value; }
}
}