title: Insert primary key from one table into another based on user input from a form
body: <p>I have a form with an <code>asp:DropDownList</code> that populates its contents from the <code>Item_Description</code> column in the <code>Item</code> table.</p>
  
  <p>I would like to allow users to select an item from the drop-down list, and when the form is submitted, insert the <code>ItemID</code> value for the selected item into the Orders table using C#/T-SQL.</p>
  
  <p>I could theoretically accomplish this with some lengthy if/else statements, but this seems too simple to require so much code.</p>
  
  <p>I am able to update other tables by assigning <code>asp:Textbox</code> values to parameters and inserting them upon submission. I just can't seem to figure out how to pass the <code>ItemID</code> to the <code>Orders</code> table when a user selects a related item from the list.</p>
  
  <p>P.S. I have already established a connection to my database, protected against SQL injection, etc.</p>
  
  <p>Drop-down list:</p>
  
  <p>ID="ddlItems"</p>
  
  <p>Code for tables:</p>
  
  <pre><code>CREATE TABLE Item
  (
      ItemID int primary key,
      Item_Description varchar(255)
  ); 
  
  CREATE TABLE Orders
  (
      OrderID int primary key,
      Order_Item varchar(255)
  );
  </code></pre>
  
  <p>Code-behind method for Submit button click event:</p>
  
  <pre><code>protected void Submit_Click(object sender, EventArgs e)
  {
      SqlConnection conn;
      SqlCommand comm;
      SqlDataReader reader;
      conn = new SqlConnection("Server=*omitted*");
      comm = new SqlCommand("INSERT INTO Orders VALUES @ItemID;", conn);
  
      comm.Parameters.Add("@ItemID", System.Data.SqlDbType.Int);
      comm.Parameters["@ItemID"].Value = &lt;&lt;&lt;This is where I need help&gt;&gt;&gt;;
  
      try
      {
          conn.Open();
          reader = comm.ExecuteReader();
          reader.Close();
      }
      catch
      {
          results.Text = "Error adding data. ";
      }
      finally
      {
          // Close the connection
          conn.Close();
          results.Text = "Added data.";
      }
  }
  </code></pre>
  
  <p>Drop-down List binding:</p>
  
  <pre><code>&lt;asp:DropDOwnList ID="ddlItems" runat="server" AutoPostBack="True" AppendDataBoundItems="True" DataSourceID="SqlDataSource1" DataTextField="Item_Description" DataValueField="Item_Description"&gt;
  &lt;asp:ListItem Value=""&gt;&lt;/asp:ListItem&gt;
  &lt;asp:DropDownList&gt;
  </code></pre>