日期:2014-05-17  浏览次数:20445 次

关于asp.net里面数据验证
我现在在做一个添加的操作。
现在有一个问题,就是我现在的代码,不会限制我重复插入数据,比如UserID为1的员工,用现在下面的办法,可以重复插入几十条数据。
所以我现在想做一个判断,提交时,当UserID在数据库中已经存在,就弹出提示框,说明数据已存在,无法添加,如果不存在,就继续添加操作
我现在想到sql语句:select count(*) from 表名 where UserID='"+ddlUserID.SelectedValue+"'
如果>1就报提示框,如果=0就添加成功,但是具体的语句不清楚要怎么写哦,有谁能告诉我下哦
protected void btnsave_Click(object sender, EventArgs e)
        {
            if (ViewState["showType"] != null)
            {
                UP.GroupID = Convert.ToInt32(ddlGroupID.SelectedValue);
                UP.Name = ddlUCName.SelectedValue;
                UP.UserID = Convert.ToInt32(ddlUserID.SelectedValue);
                UP.Duty = ddlDuty.SelectedValue;
                UP.Lv = Convert.ToInt32(ddlLv.SelectedValue);

                
                if (ViewState["showType"].ToString() == "ADD")
                {                   
                    int result = UserPermissionBLL.InsertUserPermission(UP);
                    if (result > 0)
                    {
                        Response.Write("<script> alert('添加成功');window.location.href='UserPermission.aspx';</script>");
                    }
                    }
                }