title: Inserting values into table from textboxes & from another table's foreign key C#
body: <p>Im trying to insert into a table values from textboxes AND to retrieve a foreign key id from another table and insert that also.</p>
  
  <p>So I have a users table which contains UserId and I want this inserted into a nutrition diary which includes all the data that comes from textboxes(ie Weight, Height, Date etc)</p>
  
  <p>I am retrieving the MemberId by using a session to track the username(lblRegistered)
  Here is my code:</p>
  
  <pre><code>SqlConnection con = new SqlConnection("path for my connection");
  con.Open();
  
  SqlCommand cmd = new SqlCommand("select COUNT(*)FROM Members where Username='" + lblRegistered.Text + "'", con);
  
  con.Close();
  
  con.Open();
  
  cmd.CommandText = "INSERT INTO Nutrition(Weight, Height, Bmi, Date, WaterIntake, CalorieIntake, MemberId) values ('" + txtWeight.Text + "','" + txtHeight.Text + "','" + txtBmi.Text + "','" + txtDate.Text + "','" + txtWater.Text + "','" + txtCalorie.Text + "', Select Users.UserId From Users where (Users.Username= '" + lblRegistered.Text + "'))";
  
  cmd.ExecuteNonQuery();
  cmd.Clone();
  con.Close();
  Response.Redirect("Success.aspx");
  </code></pre>
  
  <p>The error is close to <strong>Select</strong> Users part.</p>
  
  <p>Any help would be greatly appreciated!</p>