如何在“添加”按钮中插入对数据库中值的验证
我想实现的是在往数据库中插入内容的时候,首先对要插入的内容进行验证,看数据库是否存在,我采用的是从gridview中先提取它的货号进行比较,但是我就是不知道怎么用代码实现和数据库中的货号比较,
protected void btchaxun_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConStr"]);
conn.Open();
SqlCommand mycommand = new SqlCommand("select count(*) from commoditylist where goodsno ='" + huohao + "'", conn);
int count = Convert.ToInt32(mycommand.ExecuteScalar());
if (count > 0)
{
lb1.Text = "查无此货";
}
else
{
// SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConStr"]);
// conn.Open();
SqlDataAdapter comm = new SqlDataAdapter("select * from commoditylist where goodsno= " + Convert.ToInt32(huohao.Text), conn);
DataSet dc = new DataSet();
comm.Fill(dc, "commoditylist");
dr = dc.Tables["commoditylist"].Rows[0];
pinming.Text = dr["hname"].ToString();
guige.Text = dr["specifications"].ToString();
chandi.Text = dr["origin"].ToString();
DropDownList1.SelectedItem.Text = dr["categories"].ToString();
jinhuojia.Text = dr["stockprice"].ToString();
xiaoshoujia.Text = dr["saleprice1"].ToString();
zuidishoujia.Text = dr["minimumsaleprice"].ToString();
kucunxiaxian.Text = dr["kcxx"].ToString();
beizhu.Text = dr["note"].ToString();
}
}
我是这样写的啊 但是它总是报错啊