Parameter.addwithvalue - ExecuteReader:CommandText属性尚未初始化

时间:2013-03-12 20:05:00

标签: c# asp.net sqlcommand

我在adapter.fill(ds)行收到错误 ExecuteReader:CommandText属性尚未初始化。奇怪的是,如果我用实际的字符串(例如'name')替换@user,它的工作完全正常,所以在设置参数的部分中似乎有些东西被打破了。

我尝试使用和不使用'设置字符串(即@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();

}
}

2 个答案:

答案 0 :(得分:2)

您可能会在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);

答案 1 :(得分:2)

修改忘记在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();

    }