【问题标题】:Parameter.addwithvalue - ExecuteReader: CommandText property has not been initializedParameter.addwithvalue - ExecuteReader:CommandText 属性尚未初始化
【发布时间】:2013-02-28 13:05:17
【问题描述】:

我在adapter.fill(ds) 行收到错误ExecuteReader:CommandText 属性尚未初始化。奇怪的是,如果我用一个实际的字符串(例如'name')替换@user,它工作得非常好,所以设置参数的部分似乎有问题。

我尝试设置带有和不带有''s 的字符串(即@user/'@user')。我也尝试过同时使用=likeUser.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();

}
}

【问题讨论】:

    标签: c# asp.net sqlcommand


    【解决方案1】:

    编辑忘记一切,只需在 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();
    
        }
    

    【讨论】:

    • 我不明白这个答案——具体的解决方案是什么?
    • @PandaWood 我这样做已经有好几年了,所以我不能确定,但​​我认为这类似于参数设置不正确,运行他建议的行会让你看到。因此,解决方法是确保您设置“@user”的值实际上具有值。但我根本不记得这个...... :'D
    【解决方案2】:

    您可能会在command.Parameters.AddWithValue("@user", User.Identity.Name.ToString()); 中获得null

    试试:

    command.Parameters.AddWithValue("@user", User.Identity.Name ?? String.Empty);
    

    编辑:(在 cmets 之后)

    您是否尝试过手动创建参数?

    不要使用AddWithValue(...)试试:

    SqlParameter param  = new SqlParameter();
    param.ParameterName = "@user";
    param.Value         = User.Identity.Name.ToString();
    param.DbType        =  SqlDbType.VarChar;
    command.Parameters.Add(param);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-02
      • 2021-06-21
      • 2017-03-03
      • 2012-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多