我收到错误ExecuteReader: CommandText property has not been initialized在该adapter.fill(ds)
行。奇怪的是,如果我@user
用一个实际的字符串(例如'name')替换它,它工作得非常好,所以在设置参数的部分似乎有些东西被破坏了。
我尝试设置带有和不带有'
's 的字符串(即@user/'@user')。我也尝试过同时使用=
和like
。User.Identity.Name.ToString()
已经过测试,可以通过为其设置一个文本框来正确返回登录用户。
对不起,非英语数据库变量,如果这个问题已经在某处得到回答。不过,经过六个小时的搜索,我几乎放弃了(也许我只是很烂)。
相关代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
public partial class Bruker : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String conn = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
SqlConnection sql = new SqlConnection(conn);
sql.Open();
SqlCommand command = new SqlCommand(conn);
command.CommandText = "SELECT klubb.KlubbNavn FROM Klubber klubb inner join User_Klubber_Connection conn on Klubb.Klubb_Id = conn.Klubb_Id inner join aspnet_Users bruker on bruker.UserId = conn.UserId WHERE bruker.UserName = @user";
command.Parameters.AddWithValue("@user", User.Identity.Name.ToString());
command.Connection = sql;
SetDropDownList(command);
DropDownList1.SelectedIndex = 0;
ChangeGridView(GetMembersOfClub(), sql);
sql.Close();
}
protected void SetDropDownList(SqlCommand command)
{
SqlDataAdapter adapter = new SqlDataAdapter(command);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
DataSet ds = new DataSet();
adapter.Fill(ds);
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "KlubbNavn";
DropDownList1.DataBind();
}
}