【发布时间】:2016-06-02 16:45:59
【问题描述】:
在这里,我试图根据 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;
}
【问题讨论】:
-
有些事情并不像你想的那样,因为如果表 tblUser 中确实存在名为“FullName”的列,该代码应该不会失败。
-
检查您的连接字符串。您可能在连接字符串中使用了错误的数据库。
-
txttfullname.Text = dt.Rows[0]["FullName"].ToString();用 dc 替换 dt 应该可以正常工作。