ExecuteReader:Connection属性尚未初始化错误

时间:2014-08-27 11:47:49

标签: asp.net ado.net

我有一个ExecuteReader错误:Connection属性尚未初始化第28行:gvEmployeeDetail.DataSource = cmdSelectWithUsername.ExecuteReader();

 ExecuteReader: Connection property has not been initialized
Line 28:          gvEmployeeDetail.DataSource = cmdSelectWithUsername.ExecuteReader();

我的代码在这里

protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["New"] != null)
        {
            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            using (SqlConnection con = new SqlConnection(CS))
            {
                lblUserNamed.Text = Session["New"].ToString();
                SqlCommand cmdSelectWithUsername = new SqlCommand("select* from tblEmployeeDetail where UserName = @username ");

                cmdSelectWithUsername.Parameters.AddWithValue("@username", lblUserNamed.Text);
                con.Open();
                gvEmployeeDetail.DataSource = cmdSelectWithUsername.ExecuteReader();
                gvEmployeeDetail.DataBind();

            }
        }

1 个答案:

答案 0 :(得分:0)

分配与命令的连接

赞:

SqlCommand cmdSelectWithUsername = new SqlCommand("select* from tblEmployeeDetail where
   UserName = @username ", con);