Sql数据读取器不会将我的值返回给对象

时间:2013-07-26 07:34:15

标签: c# asp.net oop

我有这个代码从SQL获取返回查询并将其放在标签中。问题是,当我点击搜索按钮时,标签不会填充。但是当我进入调试模式时,它有值。这是为什么?!帮助!

//I've cut down the code here to make the question clearer
//But these 4 fields were all valid properties - Sayse
public string FirstName
public string MiddleName
public string LastName
public string Email

  public void SearchAffectedUser()
    {
        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
        {

            using (SqlCommand com_retrieve = new SqlCommand("Reader.usp_SearchAffectedUser", con))
            {
                com_retrieve.CommandType = CommandType.StoredProcedure;
                com_retrieve.Parameters.Add("@fld_EmployeeId", SqlDbType.Int).Value = AffectedUser;
                con.Open();

                com_retrieve.ExecuteNonQuery();
                SqlDataReader dr = com_retrieve.ExecuteReader();
                if (dr.Read())
                {

                    FirstName = dr.GetValue(0).ToString();
                    MiddleName = dr.GetValue(1).ToString();
                    LastName = dr.GetValue(2).ToString();
                    Email = dr.GetValue(3).ToString();
                }

            }
        }
    }

  protected void findAffectedUserButton_Click(object sender, EventArgs e)
    {
        ticket.AffectedUser = affectedUserTextBox.Text;
        ticket.FirstName = firstNameValueLabel.Text;
        ticket.MiddleName = middleNameValueLabel.Text;
        ticket.LastName = lastNameValueLabel.Text;
        ticket.Email = emailValueLabel.Text;
        ticket.SearchAffectedUser();

    }

2 个答案:

答案 0 :(得分:3)

首先设置用于搜索的AffectedUser属性,然后调用搜索命令,最后设置标签。

protected void findAffectedUserButton_Click(object sender, EventArgs e)
{
    ticket.AffectedUser = affectedUserTextBox.Text;
    ticket.SearchAffectedUser();
    firstNameValueLabel.Text = ticket.FirstName;
    middleNameValueLabel.Text = ticket.MiddleName;
    lastNameValueLabel.Text = ticket.LastName;
    emailValueLabel.Text = ticket.Email;

}

答案 1 :(得分:0)

将标签 TEXT 属性设置为“”。