4

我收到错误ExecuteReader: CommandText property has not been initialized在该adapter.fill(ds)行。奇怪的是,如果我@user用一个实际的字符串(例如'name')替换它,它工作得非常好,所以在设置参数的部分似乎有些东西被破坏了。

我尝试设置带有和不带有''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();

}
}
4

2 回答 2

2

可能你要null进去了command.Parameters.AddWithValue("@user", User.Identity.Name.ToString());

尝试:

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

编辑:(评论后)

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

而不是使用AddWithValue(...)try :

SqlParameter param  = new SqlParameter();
param.ParameterName = "@user";
param.Value         = User.Identity.Name.ToString();
param.DbType        =  SqlDbType.VarChar;
command.Parameters.Add(param);
于 2013-03-12T20:09:00.033 回答
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();

    }
于 2013-03-12T20:38:33.130 回答