求救各位高手

ddlNewType”有一个无效 SelectedValue,因为它不在项目列表中。
参数名: value

说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.ArgumentOutOfRangeException: “ddlNewType”有一个无效 SelectedValue,因为它不在项目列表中。
参数名: value

源错误:

行 359:        this.txtNewCreateP.Text = uInfo.CreatePerson.Trim();
行 360:        this.txtMemo.Text = uInfo.Memo.Trim();
行 361:        this.ddlNewType.SelectedValue = uInfo.Type.Trim();
行 362:    }
行 363:    /// <summary>

 protected void btnCreateUser_Click(object sender, EventArgs e)
    {
        try
        {
            this.clearErrorMsg();
            //进入新建模式
            this.insertMode();
            //显示新建用户区域
            this.divUserMng.Visible = true;

        }
        catch
        {
            throw;
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            this.clearErrorMsg();
            //获得用户输入的数据
            UserInfo uInfo = this.getNewUserInfo();
            //调用bll层方法,插入数据
            userService.InsertUserInfo(uInfo);
            //保存完毕,从新绑定数据
            this.gvDataBind();
            this.insertMode();
        }
        catch(Exception ex)
        {
            this.lblCreateError.Text = ex.Message;
        }
    }

   /// <summary>
   /// 将界面上的信息封装为UserInfo对象
   /// </summary>
   /// <returns></returns>
    private UserInfo getNewUserInfo()
    {
        UserInfo uInfo = new UserInfo();
        uInfo.UserId = this.txtNewUID.Text.Trim();
        uInfo.Pwd = this.txtNewPwd.Text.Trim();
        uInfo.Type = this.ddlNewType.SelectedValue.ToString().Trim();
        uInfo.CreateDate = this.txtNewCreateD.Text.Trim();
        uInfo.CreatePerson = this.txtNewCreateP.Text.Trim();
        uInfo.Memo = this.txtMemo.Text.Trim();
        return uInfo;
    }

    #endregion

    #region 界面模式

    /// <summary>
    /// 将详细信息区域设置为新建模式
    /// </summary>
    private void insertMode()
    {
        this.clearErrorMsg();
        this.txtNewCPwd.Text = "";
        this.txtNewCreateP.Text = "";
        this.txtNewPwd.Text = "";
        this.txtNewUID.Text = "";
        this.txtMemo.Text = "";
        this.txtNewCreateD.Text = DateTime.Now.ToString().Trim();
        this.ddlNewType.SelectedValue = "";
        this.setControlStatus(true);
        this.btnSaveUpdate.Visible = false;
       
    }

    private void detailMode()
    {
        this.clearErrorMsg();
        this.divUserMng.Visible = true;
        this.setControlStatus(false);
       
    }

    private void editMode()
    {
        this.clearErrorMsg();
        this.divUserMng.Visible = true;

        this.setControlStatus(true);
        this.txtNewUID.ReadOnly = true;
        this.btnSave.Visible = false;
    }

    /// <summary>
    /// 设置控件的状态
    /// </summary>
    /// <param name="Status">true:控件可用,false :控件不可用
    /// </param>
    private void setControlStatus(bool Status)
    {
        this.btnSave.Visible = Status;
        this.btnSaveUpdate.Visible = Status;
        this.ddlNewType.Enabled = Status;
        this.txtMemo.ReadOnly = !Status;
        this.txtNewCPwd.ReadOnly =! Status;
        this.txtNewCreateP.ReadOnly = !Status;
        this.txtNewPwd.ReadOnly = !Status;
        this.txtNewUID.ReadOnly = !Status;
        this.txtNewCreateD.ReadOnly = !Status;
    }

    #endregion

    #region 显示详细信息、编辑更新用户信息
    protected void gvUserInfo_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "Detail")
            {
                this.detailMode();
            }
            //编辑用户信息
            if (e.CommandName == "Edit")
            {
                //进入编辑模式
               
                this.editMode();
            }

            UserInfo uInfo = null;
            UserInfoSearchConds searchConds = new UserInfoSearchConds();
            //获取用户ID
            searchConds.UserId = gvUserInfo.Rows[Convert.ToInt32(e.CommandArgument)].Cells[1].Text.ToString().Trim();
            ArrayList infoList = userService.GetUserInfos(searchConds);
            if (infoList.Count != 0)
            {
                uInfo = (UserInfo)infoList[0];
            }

            this.controlsBind(uInfo);
        }
        catch
        {
            throw;
        }
    }

    /// <summary>
    /// 将得到的信息绑定到界面相应的控件上
    /// </summary>
    /// <param name="uInfo"></param>
    private void controlsBind(UserInfo uInfo)
    {
        this.txtNewUID.Text = uInfo.UserId.Trim();
        this.txtNewPwd.Text = uInfo.Pwd.Trim();
        this.txtNewCPwd.Text = uInfo.Pwd.Trim();
        this.txtNewCreateD.Text = uInfo.CreateDate.Trim();
        this.txtNewCreateP.Text = uInfo.CreatePerson.Trim();
        this.txtMemo.Text = uInfo.Memo.Trim();
        this.ddlNewType.SelectedValue = uInfo.Type.Trim();
    }
    /// <summary>
    /// 保存编辑
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSaveUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            //清除界面上的错误信息
            this.clearErrorMsg();
            //获取用户输入的数据并封装为对象,是否有现成的方法可用?
            UserInfo uInfo=this.getNewUserInfo();
            //是否进行有效性检查?何种检查?如何设计?
            if (txtNewPwd.Text == null || txtNewPwd.Text == "" || this.ddlNewType.SelectedValue == null || this.ddlNewType.SelectedValue == "")
            {
                this.lblCreateError.Text = "密码和类型为必填字段,请检查是否正确填写!";
            }
            else
            {
                //调用BLL层更新用户信息的方法
                userService.UpdateUserInfo(uInfo);
               
                //进行数据绑定
                this.gvDataBind();
                //提示用户更新成功
                this.lblCreateError.Text = "更新用户信息成功!";
            }
            //更新成功后应该将界面设为何种模式?

        }
        catch
        {
            throw;
        }
    }

DLL中的代码

namespace DAL.UserLogin
{
    public  class UserLoginDAO
    {
        /// <summary>
        /// 根据用户名和密码查询
        /// </summary>
        private const string SQL_SELECT_USER_LOGIN_INFO = "select * from tblUserInfo where userId='{0}' and pwd='{1}'";
        private const string SQL_SELECT_USER_INFO_LIST = "SELECT USERID,PWD,USERTYPE,CREATEDATE,CREATEPERSON,MEMO FROM TBLUSERINFO";
        private const string SQL_DELETE_ACCOUNT_INFO = "delete from TBLUSERINFO where userid = '{0}'";
        private const string SQL_INSERT_USER_INFO = "insert into tblUserInfo ";
        private const string SQL_SELECT_USER_TYPE = "select distinct userType from tblUserInfo";
        //更新用户的SQL语句 update 表名 set 列名=新值,列名=新值 where userId=要更新的值
        private const string SQL_UPDATE_USER_INFO = "update tblUserInfo set ";

        /// <summary>
        /// 按用户名和密码查询用户信息,用于用户登录时,判断用户是否存在
        /// </summary>
        /// <param name="UserId">用户名</param>
        /// <param name="Pwd">密码</param>
        /// <returns></returns>
        public UserInfo SignIn(string UserId, string Pwd)
        {
            //处理SQL字符串
            string sqlStr = String.Format(SQL_SELECT_USER_LOGIN_INFO, UserId, Pwd);

            //定义UserInfo对象
            UserInfo loginInfo = null;
            try
            {
                //定义SqlDataReader,并调用DBConnHelper中的方法读取数据
                using (SqlDataReader rdr = DBConnHelper.ExecuteReader(DBConnHelper.ConnString, CommandType.Text, sqlStr, null))
                {
                    //Read()方法,返回值bool型,如果存在行,则为 true;否则为 false。
                    if (rdr.Read())
                    {
                        //调用本类地方法DataMapping,将数据库读取出的数据赋值给UserInfo对象
                        loginInfo = this.DataMapping(rdr);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return loginInfo;
        }

        /// <summary>
        /// 把数据库中取出的数据集转换为数据对象UserInfo
        /// </summary>
        /// <param name="rdr"></param>
        /// <returns></returns>
        private UserInfo DataMapping(SqlDataReader rdr)
        {
           //实例化一个UserInfo对象
            UserInfo theUserInfo=new UserInfo();

            //将数据库中读出的数据依次赋值给数据对象theUserInfo的属性
            theUserInfo.UserId = rdr.GetString(0);
            theUserInfo.Pwd = rdr.GetString(1);
            theUserInfo.Type = rdr.GetString(2);

            //如果数据库中字段可能为空,需要进行判断
            if (rdr.IsDBNull(3))
            {
                //如果数据集中该字段的值不存在,则将对象的值置为空字符串
                theUserInfo.CreateDate = "";
            }
            else
            {
                theUserInfo.CreateDate = rdr.GetString(3);
            }
            if (rdr.IsDBNull(4))
            {
                theUserInfo.CreatePerson = "";
            }
            else
            {
                theUserInfo.CreatePerson = rdr.GetString(4);
            }
            if (rdr.IsDBNull(5))
            {
                theUserInfo.Memo = "";
            }
            else
            {
                theUserInfo.Memo = rdr.GetString(5);
            }
            return theUserInfo;
        }

        /// <summary>
        /// 通过查询条件查询用户信息
        /// </summary>
        /// <param name="conds">查询条件数据对象</param>
        /// <returns>用户信息列表</returns>
        public ArrayList GetUserInfoList(UserInfoSearchConds Conds)
        {
            try
            {
                ArrayList acctList = new ArrayList();
                string sqlStr = GetQuerySQL(Conds);
                //定义UserInfo对象

                //定义SqlDataReader,并调用DBConnHelper中的方法读取数据
                using (SqlDataReader rdr = DBConnHelper.ExecuteReader(DBConnHelper.ConnString, CommandType.Text, sqlStr, null))
                {
                    //Read()方法,返回值bool型,如果存在行,则为 true;否则为 false。
                    while (rdr.Read())
                    {
                        //调用本类地方法DataMapping,将数据库读取出的数据赋值给UserInfo对象
                        UserInfo loginInfo = this.DataMapping(rdr);
                        acctList.Add(loginInfo);
                    }
                }
                return acctList;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 动态生成查询条件
        /// </summary>
        /// <param name="queryCond">查询条件数据对象</param>
        /// <returns>添加了查询条件的SQL语句</returns>
        private string GetQuerySQL(UserInfoSearchConds queryCond)
        {
            StringBuilder sqlBuilder = new StringBuilder(SQL_SELECT_USER_INFO_LIST);
            sqlBuilder.Append(" where 1=1 ");
            if (queryCond.UserId != null && queryCond.UserId != "")
                sqlBuilder.Append(" and  USERID like '%" + queryCond.UserId + "%'");
            if (queryCond.Type != null && queryCond.Type != "")
                sqlBuilder.Append(" and UserType = '" + queryCond.Type + "'");
            if (queryCond.CreateDate != null && queryCond.CreateDate != "")
                sqlBuilder.Append(" and CreateDate like '" + queryCond.CreateDate + "%'");
            if (queryCond.CreatePerson != null && queryCond.CreatePerson != "")
                sqlBuilder.Append(" and CreatePerson like '" + queryCond.CreatePerson + "%'");
            sqlBuilder.Append(" order by userid");
            return sqlBuilder.ToString();
        }

        /// <summary>
        /// 通过用户id删除用户信息
        /// </summary>
        /// <param name="acctInfo">UserInfo数据模型</param>
        public void DeleteUserInfo(string userId)
        {
            string sqlDele = String.Format(SQL_DELETE_ACCOUNT_INFO, userId);

            using (SqlConnection conn = new SqlConnection(DBConnHelper.ConnString))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        {
                            DBConnHelper.ExecuteNonQuery(trans, CommandType.Text, sqlDele, null);
                            trans.Commit();
                        }
                    }

                    catch (Exception e)
                    {
                        trans.Rollback();
                        throw e;
                    }
                }
            }
        }

        #region 新建用户信息
        private string GetInsertSQL(UserInfo uInfo)
        {
            StringBuilder sqlInsertBuilder = new StringBuilder(SQL_INSERT_USER_INFO);
            sqlInsertBuilder.Append(" values('");
            if (uInfo.UserId != null && uInfo.UserId != "")
                sqlInsertBuilder.Append(uInfo.UserId + "','");
            else
                sqlInsertBuilder.Append("','");
            if (uInfo.Pwd != null && uInfo.Pwd != "")
                sqlInsertBuilder.Append(uInfo.Pwd + "','");
            else
                sqlInsertBuilder.Append("','");
            if (uInfo.Type != null && uInfo.Type != "")
                sqlInsertBuilder.Append(uInfo.Type + "','");
            else
                sqlInsertBuilder.Append("','");
            if (uInfo.CreateDate != null && uInfo.CreateDate != "")
                sqlInsertBuilder.Append(uInfo.CreateDate + "','");
            else
                sqlInsertBuilder.Append("','");
            if (uInfo.CreatePerson != null && uInfo.CreatePerson != "")
                sqlInsertBuilder.Append(uInfo.CreatePerson + "','");
            else
                sqlInsertBuilder.Append("','");
            if (uInfo.Memo != null && uInfo.Memo != "")
                sqlInsertBuilder.Append(uInfo.Memo + "'");
            else
                sqlInsertBuilder.Append("'");
            sqlInsertBuilder.Append(")");
            return sqlInsertBuilder.ToString();
        }

        /// <summary>
        /// 插入用户信息
        /// </summary>
        /// <param name="uInfo"></param>
        public void InsertUserInfo(UserInfo uInfo)
        {
            string sqlInsert = this.GetInsertSQL(uInfo);


            using (SqlConnection conn = new SqlConnection(DBConnHelper.ConnString))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        {
                            DBConnHelper.ExecuteNonQuery(trans, CommandType.Text, sqlInsert, null);
                            trans.Commit();
                        }
                    }

                    catch
                    {
                        throw;
                    }
                }
            }
        }
        #endregion

        #region 更新用户信息
        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="uInfo">需要更新的用户信息数据对象</param>
        public void UpdateUserInfo(UserInfo uInfo)
        {
            //生成SQL语句
            string sqlUpdate = this.GetUpdateSQL(uInfo);
            //执行更新操作
            using (SqlConnection conn = new SqlConnection(DBConnHelper.ConnString))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        {
                            DBConnHelper.ExecuteNonQuery(trans, CommandType.Text, sqlUpdate, null);
                            trans.Commit();
                        }
                    }

                    catch (Exception e)
                    {
                        trans.Rollback();
                        throw e;
                    }
                }
            }
          
           
        }

        /// <summary>
        /// 生成更新用户的SQL语句
        /// </summary>
        /// <param name="uInfo">需要更新的用户信息数据</param>
        /// <returns>返回生成的SQL语句</returns>
        private string GetUpdateSQL(UserInfo uInfo)
        {
            StringBuilder sqlBuilder = new StringBuilder(SQL_UPDATE_USER_INFO);
           
                sqlBuilder.Append(" UserType = '" + uInfo.Type + "'");
          
                sqlBuilder.Append(" , pwd= '" + uInfo.Pwd + "'");
        
                sqlBuilder.Append(" , CreateDate = '" + uInfo.CreateDate + "'");
    
                sqlBuilder.Append(", CreatePerson= '" + uInfo.CreatePerson + "'");
                sqlBuilder.Append(", Memo= '" + uInfo.Memo + "'");
                sqlBuilder.Append(" where userid = '" + uInfo.UserId + "'");
            return sqlBuilder.ToString();
        }
        #endregion

    }
}

  • 0
    点赞
  • 1
    评论
  • 0
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

评论1
请先登录 后发表评论~
©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页

打赏作者

zhangtaobeijin

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值