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(); } }
source share