System.InvalidOperationException: ExecuteReader: CommandText

本文关键字:CommandText ExecuteReader System InvalidOperationException | 更新日期: 2024-11-07 11:46:12

当我没有为用户中的logg定义Usertype时,我的网格视图工作正常。现在我的BindGrid()功能无法正常工作并给我错误

系统无效操作异常: 执行读取器: 命令文本属性尚未初始化 错误即将到来

这是我的 BindGrid 代码隐藏:-

protected void BindGrid()
    {
        string username = string.Empty;
        string usertype = string.Empty;
        try
        {
            SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultCSRConnection"].ConnectionString);
            SqlCommand cmd = new SqlCommand("SELECT usertype,username FROM tbl_User WHERE username='" + Session["User"].ToString() + "'", conn);
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    username = dr["username"].ToString();
                    usertype = dr["usertype"].ToString();
                }
            }
            conn.Close();
            string query = string.Empty;
            if (usertype == "0") // superadmin
            {
                query = "select Id,username,email,usertype,active,(CASE WHEN usertype='1' THEN 'Admin' WHEN usertype='0' THEN 'Super Admin' WHEN usertype='2' THEN 'User' END) AS UserRoleName from tbl_User ORDER By Id DESC";
            }
            if (usertype == "1") // admin
            {
                query = "select Id,username,email,usertype,active,(CASE WHEN usertype='1' THEN 'Admin' WHEN usertype='0' THEN 'Super Admin' WHEN usertype='2' THEN 'User' END) AS UserRoleName from tbl_User WHERE usertype != '0' ORDER By Id DESC";
            }
            if (usertype == "2") // user
            {
                query = "select Id,username,email,usertype,active,(CASE WHEN usertype='1' THEN 'Admin' WHEN usertype='0' THEN 'Super Admin' WHEN usertype='2' THEN 'User' END) AS UserRoleName from tbl_User WHERE username='" + username + "' ORDER By Id DESC";
            }
            cmd = new SqlCommand(query, conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            grdUser.DataSource = ds.Tables[0];
            grdUser.DataBind();
        }
        catch (Exception)
        {
            throw;
        }
    }

请在这里指出问题所在。

System.InvalidOperationException: ExecuteReader: CommandText

我推荐以下内容:

  • 使用 using 语句实现代码
  • 使用参数化 SQL
  • 确保初始查询返回一条记录并且只返回一条记录(您的循环将仅捕获上次遇到的usernameusertype
  • 执行switch声明并确保正确处理default案例
  • 确保为第二个 SQL 查询打开连接
  • 如果您只是重新抛出异常,则不需要 try/catch 块。

我已经添加了参数,现在可以安全使用。

protected void BindGrid()
        {
            string username = string.Empty;
            string usertype = string.Empty;
            try
            {
                SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultCSRConnection"].ConnectionString);
                SqlCommand cmd = new SqlCommand("SELECT usertype,username FROM tbl_User WHERE username=@username", conn);
                cmd.Parameters.Add("@username", SqlDbType.VarChar).Value = Session["User"].ToString();
                conn.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    if (dr.Read())
                    {
                        username = dr["username"].ToString();
                        usertype = dr["usertype"].ToString();
                    }
                }
                conn.Close();
                string query = string.Empty;
                if (!string.IsNullOrEmpty(usertype))
                {
                    if (usertype == "0") // superadmin
                    {
                        query = "select Id,username,email,usertype,active,(CASE WHEN usertype='1' THEN 'Admin' WHEN usertype='0' THEN 'Super Admin' WHEN usertype='2' THEN 'User' END) AS UserRoleName from tbl_User ORDER By Id DESC";
                    }
                    if (usertype == "1") // admin
                    {
                        query = "select Id,username,email,usertype,active,(CASE WHEN usertype='1' THEN 'Admin' WHEN usertype='0' THEN 'Super Admin' WHEN usertype='2' THEN 'User' END) AS UserRoleName from tbl_User WHERE usertype != '0' ORDER By Id DESC";
                    }
                    if (usertype == "2") // user
                    {
                        query = "select Id,username,email,usertype,active,(CASE WHEN usertype='1' THEN 'Admin' WHEN usertype='0' THEN 'Super Admin' WHEN usertype='2' THEN 'User' END) AS UserRoleName from tbl_User WHERE username='" + username + "' ORDER By Id DESC";
                    }
                    cmd = new SqlCommand(query, conn);
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    grdUser.DataSource = ds.Tables[0];
                    grdUser.DataBind();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }