title: Connecting Two SQL Server Tables
body: <p>I'm trying to connect the following 2 tables together:</p>
  
  <pre><code>CREATE TABLE [dbo].[Groups] 
  (
      [Group_Id]   INT IDENTITY (1, 1) NOT NULL,
      [Group_Name] NVARCHAR (50) NULL,
      [Group_Desc] NVARCHAR (50) NULL,
  
      PRIMARY KEY CLUSTERED ([Group_Id] ASC)
  );
  
  CREATE TABLE [dbo].[Events] 
  (
      [Event_Id] INT IDENTITY (1, 1) NOT NULL,
      [Event_Group_Id] INT null,
      [Event_Type]  NVARCHAR (50) NULL,
      [Event_Title] NVARCHAR (50) NULL,
  
      PRIMARY KEY CLUSTERED ([Event_Id] ASC),
  
      CONSTRAINT fk_event_group  
          FOREIGN KEY (Event_Group_Id) REFERENCES Groups (Group_Id)
  );
  </code></pre>
  
  <p>First question: have I created the tables correctly?</p>
  
  <p>I've been able to add to the <code>Events</code> with the following code but I haven't been able to successfully connect the PK of <code>Groups</code> as an FK in <code>Events</code>.</p>
  
  <p>Does anyone have any suggestions?</p>
  
  <pre><code>  SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["BallinoraDBConnectionString1"].ConnectionString);
          conn.Open();
          string findGroupOfEvent = "SELECT Group_Id FROM Groups WHERE Group_Name ='" + DropDownListEventGroup.SelectedItem.ToString() + "'";
          int groupId = Convert.ToInt32(findGroupOfEvent);
          string insertQuery = "INSERT INTO Events (Event_Group_Id, Event_Type, Event_Title) VALUES (@GroupEventID, @Type, @Title)";
          SqlCommand com = new SqlCommand(insertQuery, conn);
          com.Parameters.AddWithValue("@GroupEventID", groupId);
          com.Parameters.AddWithValue("@Type", DropDownListEventType.SelectedItem.ToString());
          com.Parameters.AddWithValue("@Title", TextBoxET.Text);
          com.ExecuteNonQuery();
          Response.Redirect("ViewEvents.aspx");
          Response.Write("Registration is successful");
          conn.Close();
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/v3iVI.png" rel="nofollow noreferrer">UI</a></p>
  
  <p><a href="https://i.stack.imgur.com/c0Ryz.png" rel="nofollow noreferrer">Adding Dropdown Items</a></p>