我要做的是获取订购系统的评论页面以从数据库中获取数据并将标签文本更改为数据库中的相应字段。显然我必须拥有它,以便它只使用当前登录的用户用户名而不是数据库中的第一个用户名(我使用的是asp.net成员资格数据库)。
这是后面页面的代码(我想让它显示在线上):
protected void Page_Load(object sender, EventArgs e)
{
string username1 = User.Identity.Name;
CheckUser.InsertParameters.Add("@username", username1);
string readsql =
"SELECT title, gname, sname, dob, address, suburb, postcode, dayphone, email " +
"FROM users WHERE username = @username";
using (SqlConnection myConnection = new SqlConnection(strConnString))
{
myConnection.Open();
SqlCommand myCommand = new SqlCommand(readsql, myConnection);
SqlDataReader reader = myCommand.ExecuteReader();
myCommand.ExecuteNonQuery();
reader.Read();
Label2.Text = reader["title"].ToString();
Label3.Text = reader["gname"].ToString();
Label4.Text = reader["sname"].ToString();
Label5.Text = reader.GetDateTime(reader.GetOrdinal("dob")).ToString();
Label6.Text = reader["address"].ToString();
Label7.Text = reader["suburb"].ToString();
Label18.Text = reader["postcode"].ToString();
Label8.Text = reader["dayphone"].ToString();
Label9.Text = reader["email"].ToString();
reader.Close();
myConnection.Close();
}
这是ASPX页面本身:
<asp:SqlDataSource ID="CheckUser" runat="server" ConnectionString="<%$ConnectionStrings: Database %>"
SelectCommand ="SELECT title, gname, sname, dob, address, suburb, postcode, dayphone, email FROM users WHERE username = @username" ProviderName="<%$ ConnectionStrings:Database.ProviderName %>"></asp:SqlDataSource>
我尝试了很多这方面的变化,但似乎没有任何效果。继续为@username获取Scaler Variable错误。
关于出了什么问题的任何想法?
答案 0 :(得分:2)
尝试使用
传递参数myCommand.Parameters.Add("@username").Value = username1;
答案 1 :(得分:1)
试试这个..添加你的用户名1值
SqlCommand myCommand = new SqlCommand(readsql, myConnection);
myCommand.Parameters.AddWithValue("@username", username1);
// myCommand.ExecuteNonQuery(); ------> remover this
protected void Page_Load(object sender, EventArgs e)
{
string username1 = User.Identity.Name;
string readsql = "SELECT title, gname, sname, dob, address, suburb, postcode, dayphone, email FROM users WHERE username = @username";
using (SqlConnection myConnection = new SqlConnection(strConnString))
{
myConnection.Open();
SqlCommand myCommand = new SqlCommand(readsql, myConnection);
myCommand.Parameters.AddWithValue("@username", username1);
SqlDataReader reader = myCommand.ExecuteReader();
if(reader.Read())
{
Label2.Text = reader["title"].ToString();
Label3.Text = reader["gname"].ToString();
Label4.Text = reader["sname"].ToString();
Label5.Text = reader.GetDateTime(reader.GetOrdinal("dob")).ToString();
Label6.Text = reader["address"].ToString();
Label7.Text = reader["suburb"].ToString();
Label18.Text = reader["postcode"].ToString();
Label8.Text = reader["dayphone"].ToString();
Label9.Text = reader["email"].ToString();
}
reader.Close();
myConnection.Close();
}