| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1621 人关注过本帖
标题:如何在“添加”按钮中插入对数据库中值的验证
取消只看楼主 加入收藏
馨馨
Rank: 1
等 级:新手上路
帖 子:136
专家分:0
注 册:2008-3-10
收藏
 问题点数:0 回复次数:5 
如何在“添加”按钮中插入对数据库中值的验证
我想实现的是在往数据库中插入内容的时候,首先对要插入的内容进行验证,看数据库是否存在,我采用的是从gridview中先提取它的货号进行比较,但是我就是不知道怎么用代码实现和数据库中的货号比较,
搜索更多相关主题的帖子: 中值 数据库 按钮 验证 
2008-04-14 10:48
馨馨
Rank: 1
等 级:新手上路
帖 子:136
专家分:0
注 册:2008-3-10
收藏
得分:0 
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();


        }

    }
我是这样写的啊 但是它总是报错啊
2008-04-14 11:00
馨馨
Rank: 1
等 级:新手上路
帖 子:136
专家分:0
注 册:2008-3-10
收藏
得分:0 
刚才的写错了


 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 = "查无此货";

   }
            
    }
2008-04-14 11:02
馨馨
Rank: 1
等 级:新手上路
帖 子:136
专家分:0
注 册:2008-3-10
收藏
得分:0 
运行侯的错误是

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

将 varchar 值 'System.Web.UI.WebControls.TextBox' 转换为数据类型为 int 的列时发生语法错误。
2008-04-14 11:09
馨馨
Rank: 1
等 级:新手上路
帖 子:136
专家分:0
注 册:2008-3-10
收藏
得分:0 
不是那个问题啊 ,我照你的方法做了 还是那句出错啊
2008-04-14 11:38
馨馨
Rank: 1
等 级:新手上路
帖 子:136
专家分:0
注 册:2008-3-10
收藏
得分:0 
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");这样做验证就对了,谢谢你的帮忙,
2008-04-15 11:36
快速回复:如何在“添加”按钮中插入对数据库中值的验证
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.037320 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved