Parameter.addwithvalue - ExecuteReader: CommandText property not initialized

I get an ExecuteReader error: the CommandText property was not initialized in the adapter.fill(ds) . The strange thing is that if I replaced @user with the actual string (for example, "name"), it works fine, so in the part that sets the parameter, something seems to be broken.

I tried to set the line with or without ' (i.e. @user /' @ user '). I also tried using both = and like . User.Identity.Name.ToString() was tested to correctly return the registered user by setting a text field for him.

Sorry for database variables other than English, and if this question has been answered. I almost gave up after half a dozen hours of searching, though (maybe I just sucked 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(); } } 
+4
source share
2 answers

Edit Forget everything that happens on the page_load

  Response.Write(String.Format("user name is {0}", User.Identity.Name)); 

And look at the output

works 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(); } 
+2
source

You are probably getting null in command.Parameters.AddWithValue("@user", User.Identity.Name.ToString());

Try:

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

Edit: (after comments)

Did you try to create the parameter manually?

Instead of using AddWithValue(...) try:

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

Source: https://habr.com/ru/post/1468817/


All Articles