注册 登录
编程论坛 ASP.NET技术论坛

如何在“添加”按钮中插入对数据库中值的验证

馨馨 发布于 2008-04-14 10:48, 1625 次点击
我想实现的是在往数据库中插入内容的时候,首先对要插入的内容进行验证,看数据库是否存在,我采用的是从gridview中先提取它的货号进行比较,但是我就是不知道怎么用代码实现和数据库中的货号比较,
11 回复
#2
hebingbing2008-04-14 10:58
那不就和做注册检查用户名存在的一个道理吗?
sqlcommand comm=new sqlcommand(select count(货号) from 表 where 货号字段=要检查的货号,conn);
int a=convert.toint32(comm.exevutscalar());
if(a>0)
{存在}
else
{不存在}
#3
馨馨2008-04-14 11:00
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();


        }

    }
我是这样写的啊 但是它总是报错啊
#4
馨馨2008-04-14 11:02
刚才的写错了


 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)
        {
//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();


     

         
        }
        else
        {  lb1.Text = "查无此货";

   }
            
    }
#5
馨馨2008-04-14 11:09
运行侯的错误是

int count = Convert.ToInt32(mycommand.ExecuteScalar());这一行

将 varchar 值 'System.Web.UI.WebControls.TextBox' 转换为数据类型为 int 的列时发生语法错误。
#6
hebingbing2008-04-14 11:21
SqlDataAdapter comm = new SqlDataAdapter("select * from commoditylist where goodsno= " + Convert.ToInt32(huohao.Text), conn);
应该是这行的错误吧……
不是说将 varchar 值 'System.Web.UI.WebControls.TextBox' 转换为数据类型为 int 的列时发生语法错误吗?
你看你写的对不对啊,是不是疏忽了引号啊……
SqlDataAdapter comm = new SqlDataAdapter("select * from commoditylist where goodsno= '" + Convert.ToInt32(huohao.Text)+"'", conn);
#7
馨馨2008-04-14 11:38
不是那个问题啊 ,我照你的方法做了 还是那句出错啊
#8
hebingbing2008-04-14 12:07
那句有什么错误啊……
那句不应该有那样的错误吧……
#9
hebingbing2008-04-14 12:10
奥……
找到了……
 SqlCommand mycommand = new SqlCommand("select count(*) from commoditylist where goodsno ='" + huohao + "'", conn);
这句是不是应该给huohao加上给huohao.text.toint32()
要是你的goodsno是字符那么就是huohao.text.tostring()
#10
馨馨2008-04-15 11:36
protected void queding_Click(object sender, EventArgs e)
    {
        if ( pinming.Text== "" && guige.Text == "" && chandi.Text =="" && jinhuojia.Text == ""&& xiaoshoujia.Text == "" && zuidishoujia.Text == "" && kucunxiaxian.Text == "" && beizhu.Text == "")
        {

            Response.Write("<script language='javascript'>alert('不能添加空项!')</script>");
            return;

        }


        if (exist())
        {
            // this.Label1.Text = "";
            Response.Write("<script language='javascript'>alert('该商品已经存在!')</script>");
        }
        else
        {
            insert();
        }
    }
        private Boolean exist()
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConStr"]);
        conn.Open();
         SqlCommand mycommand = new SqlCommand("select * from commoditylist where hname ='" + pinming.Text + "'", conn);
        SqlDataReader dd;
        dd = mycommand.ExecuteReader();
        if (dd.HasRows)
        {
            return true;
        }
        else
        {
            return false;
        }
      
      
   
    }
    private void insert()
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConStr"]);
        conn.Open();
        SqlCommand comm = new SqlCommand("insert into commoditylist values ( '" + pinming.Text.ToString() + "','"+ guige.Text.ToString() + "', '" + chandi.Text.ToString() + "','" + DropDownList1.SelectedItem.Value + "', '" + jinhuojia.Text + "', '" + xiaoshoujia.Text + "' ,'" + zuidishoujia.Text + "','" + kucunxiaxian.Text + "','" + beizhu.Text.ToString() + "')", conn);
        
        comm.ExecuteNonQuery();
        conn.Close();
        Response.Redirect("ziliao.aspx");这样做验证就对了,谢谢你的帮忙,
#11
xqyy2008-04-15 19:04
thisView .RowFilter =
"xx >= '" + xx + "' " ;
#12
xqyy2008-04-15 19:05
thisView .RowFilter =
"xx >= '" + xx + "' " ;
1