locked
The multi-part identifier could not be bound RRS feed

  • Question

  • User972233924 posted

    So I'm trying to retrieve the session variable (email) of the logged in user, and then use the variable in a SQL statement to retrieve the UserID of that user and place it in a read-only textbox. I can get the session variable and put that value in the textbox but I also need the ID associated with the session(email).

    After searching and trying 2 separate forum posts and <g class="gr_ gr_460 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" id="460" data-gr-id="460">analysing</g> the breakpoints they both work to an extent but then I get the same error on both (System.Data.SqlClient.SqlException: 'The multi-part identifier "xxxxxx@gmail.com" could not be bound'. From reading the error is mostly associated with problems joining tables but my blocks of code don't do such a thing.

    Here are the 2 separate blocks of code so if anyone could shed some light on why the error is appearing I would appreciate it hugely.

    using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString))
            {
                using (SqlCommand command = new SqlCommand("select UserID from Users where Email = " + Session["User"], connection))
                {
                    connection.Open();
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            txtUserID.Text = reader[0].ToString();
                        }
                    }
                }
            }
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString);
            SqlCommand cmd = new SqlCommand("select UserID from Users where Email = " + Session["User"]);
            SqlDataAdapter ap = new SqlDataAdapter(cmd.CommandText, conn);
            DataSet ds = new DataSet();
            conn.Open();
            ap.Fill(ds);
            txtUserID.Text = ds.Tables[0].Rows.Count.ToString();
            conn.Close();

    Regards,

    Mark

    Tuesday, February 6, 2018 9:55 PM

Answers

  • User972233924 posted

    For anyone wondering the SQL statement should be like this,

    "select UserID from Users where Email = '" + Session["User"] + "'"

    Works for both variations

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 7, 2018 2:28 PM

All replies

  • User972233924 posted

    For anyone wondering the SQL statement should be like this,

    "select UserID from Users where Email = '" + Session["User"] + "'"

    Works for both variations

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 7, 2018 2:28 PM
  • User-1838255255 posted

    Hi Markk25,

    According to your description and code, I think you have solved your issue, I hope you could mark your reply as answer. If not, please let us know. 

    Best Regards,
    Eric Du 

    Thursday, February 8, 2018 2:29 AM