[英]Parameter.addwithvalue - ExecuteReader: CommandText property has not been initialized
我收到错误ExecuteReader:CommandText属性尚未在adapter.fill(ds)
行初始化 。 奇怪的是,如果我将@user
替换为实际的字符串(例如'name'),它的工作原理非常好,因此在设置参数的部分中似乎会出现问题。
我试着用和不用'
('@ 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();
}
}
可能你在command.Parameters.AddWithValue("@user", User.Identity.Name.ToString());
得到null
command.Parameters.AddWithValue("@user", User.Identity.Name.ToString());
尝试:
command.Parameters.AddWithValue("@user", User.Identity.Name ?? String.Empty);
编辑:(评论后)
您是否尝试手动创建参数?
而不是使用AddWithValue(...)
尝试:
SqlParameter param = new SqlParameter();
param.ParameterName = "@user";
param.Value = User.Identity.Name.ToString();
param.DbType = SqlDbType.VarChar;
command.Parameters.Add(param);
编辑忘记在page_load上执行的所有操作
Response.Write(String.Format("user name is {0}", User.Identity.Name));
并看到输出
它运行良好
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection sql = new SqlConnection( ConfigurationManager.ConnectionStrings["yourConnectionName"].ConnectionString);
sql.Open();
SqlCommand command = new SqlCommand("Select * from userinfo where uloginid=@user", sql);
command.Parameters.AddWithValue("@user", User.Identity.Name.ToString());
SetDropDownList(command);
DropDownList1.SelectedIndex = 0;
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 = "uFirstName";
DropDownList1.DataBind();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.