Last post Feb 08, 2018 02:29 AM by Eric Du
Feb 06, 2018 09:55 PM|Markk25|LINK
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 analysing 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 "firstname.lastname@example.org" 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))
using (SqlDataReader reader = command.ExecuteReader())
txtUserID.Text = reader.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();
txtUserID.Text = ds.Tables.Rows.Count.ToString();
Feb 07, 2018 02:28 PM|Markk25|LINK
For anyone wondering the SQL statement should be like this,
"select UserID from Users where Email = '" + Session["User"] + "'"
Works for both variations
Feb 08, 2018 02:29 AM|Eric Du|LINK
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.