列不属于表?

时间:2016-06-02 07:20:48

标签: c# asp.net

这里我试图根据tblUser中也存在的用户名从tblUser检索用户的全名,并在文本框中显示。 但它显示错误列'FullName'不属于表,即使表中存在列FullName和UserName。 使用的代码

 <asp:TextBox ID="txttfullname" CssClass="form-control" runat="server"></asp:TextBox>

代码背后

DataTable dc = ojc.GetUser(lblusername.Text);
if (dc.Rows.Count > 0)
 {
      txttfullname.Text = dt.Rows[0]["FullName"].ToString();
 }

public DataTable GetUser(string UserName)
{
    SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["myconnection"].ConnectionString);
    string sql = "select *from tblUser where UserName=@UserName";
    SqlCommand cmd = new SqlCommand(sql, con);
    cmd.Parameters.AddWithValue("@UserName", UserName);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt);

    return dt;
}

1 个答案:

答案 0 :(得分:0)

这部分是对还是错字?

DataTable dc = ojc.GetUser(lblusername.Text);
if (dc.Rows.Count > 0)
 {
      txttfullname.Text = dt.Rows[0]["FullName"].ToString();
 }

获取数据表dc并绑定dt

另一个问题

lblusername.Textdc是否包含值,是否进行了调试和检查?