c# - 系统.InvalidOperationException : ExecuteReader: CommandText property has not been initialized Error is coming

标签 c# asp.net gridview

当我没有为登录用户定义 Usertype 时,我的 Gridview 工作正常。现在我的 BindGrid() 函数无法正常工作,并给我错误信息

System.InvalidOperationException: ExecuteReader: CommandText property has not been initialized Error is coming

下面是我的 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;
        }
    }

请指出这里有什么问题。

最佳答案

我推荐以下内容:

关于c# - 系统.InvalidOperationException : ExecuteReader: CommandText property has not been initialized Error is coming,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28943744/

相关文章:

c# - 允许通过带有 stringformat 和 _ 的键进行 WPF 按钮导航

c# - 用于自定义身份验证的 HttpModule 将如何与 Windows 身份验证交互?

java - 如何以特定的项目点击次数翻转 GridView 中的所有项目

c# - 从类库访问日志记录功能

c# - 在哪里存储外部 DLL 文件?

c# - 将数据导出到 excel 文件 2003、2007 及更高版本

c# - ASP.Net MVC2 (RTM) 中断响应过滤 - "Filtering is not allowed"

c# - 启用 gridview 排序时,在运行时更改 Gridview 中的列名称

c# - 将 csv 文件导入现有的 gridview

c# - Microsoft Graph API - DriveItem 上的权限不起作用