C# Parameter.addwithvalue - ExecuteReader:CommandText 属性尚未初始化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15371041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parameter.addwithvalue - ExecuteReader: CommandText property has not been initialized
提问by user1339253
I get the error ExecuteReader: CommandText property has not been initializedat the adapter.fill(ds)
line. The weird thing is that if I replace @user
with an actual string (e.g. 'name') it works perfectly fine, so something seems to be broken in the part that sets the parameter.
我收到错误ExecuteReader: CommandText property has not been initializedat the adapter.fill(ds)
line。奇怪的是,如果我@user
用实际的字符串(例如“名称”)替换它,它就可以正常工作,因此设置参数的部分似乎有问题。
I've tried to set the string both with and without '
's (i.e. @user/'@user'). I've also tried using both =
and like
. User.Identity.Name.ToString()
has been tested to return the logged in user correctly by setting a textbox to it.
我试图设置带有和不带有'
' 的字符串(即@user/'@user')。我也试过同时使用=
和like
。User.Identity.Name.ToString()
已经过测试,可以通过设置文本框来正确返回登录用户。
Sorry for the non-English database variables and if this question has been answered somewhere. I've almost given up after half a dozen hours of searching, though (maybe I just suck at it).
抱歉非英语数据库变量以及此问题是否已在某处得到解答。不过,经过六个小时的搜索,我几乎放弃了(也许我只是很烂)。
Relevant code:
相关代码:
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();
}
}
采纳答案by Siz S
EditForget everything just do following on page_load
编辑忘记一切,只需在 page_load 上执行以下操作
Response.Write(String.Format("user name is {0}", User.Identity.Name));
And see the output
并查看输出
its running fine
它运行良好
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();
}
回答by Retired_User
Probably you're getting null
in command.Parameters.AddWithValue("@user", User.Identity.Name.ToString());
也许你得到null
的command.Parameters.AddWithValue("@user", User.Identity.Name.ToString());
Try:
尝试:
command.Parameters.AddWithValue("@user", User.Identity.Name ?? String.Empty);
Edit: (after comments)
编辑:(评论后)
Have you tried to create a parameter manually?
您是否尝试过手动创建参数?
Instead of using AddWithValue(...)
try :
而不是使用AddWithValue(...)
try :
SqlParameter param = new SqlParameter();
param.ParameterName = "@user";
param.Value = User.Identity.Name.ToString();
param.DbType = SqlDbType.VarChar;
command.Parameters.Add(param);