title: How to check if ID of one table is equal to ID of another table, if equal assign new value to another column in the table in asp.net
body: <p>I am trying to build a quiz application. I have created two tables : </p>
  
  <pre><code>CREATE TABLE [dbo].[Answers] (
         [AID]           INT            NOT NULL,
         [QID]           INT            NOT NULL,
         [Answer]        NVARCHAR (200) NOT NULL,
         [CorrectAnswer] NVARCHAR (200) NULL,
         PRIMARY KEY CLUSTERED ([AID] ASC),
         CONSTRAINT [FK_Answers_Question] FOREIGN KEY ([QID]) REFERENCES [dbo].         
         [Question] ([QID])
          );
  
  CREATE TABLE [dbo].[Question] (
         [QID]      INT            IDENTITY (1, 1) NOT NULL,
         [Question] NVARCHAR (200) NOT NULL,
         PRIMARY KEY CLUSTERED ([QID] ASC)
   );
  </code></pre>
  
  <p>Here, QID is autogenerated and incremented.
  The Answer Table contains AID as primary key And QID as foreign key. 
  Now my question arises is Should I autogenerate and increment Answers.QID or should I set it separately? And also I want Answers.AID to increment upto 4 (Multiple Choice Question with 4 options).
  Something like this :</p>
  
  <pre><code>Answers Table                         
   AID | QID | Answer
   1 | 1 | Scripting Language
   2 | 1 | Programming Language
   3 | 1 | MarkUp Language
   4 | 1 | Network Protocol
  
  Question Table
   QID | Question
   1 | What is HTML?
  </code></pre>
  
  <p>So whenever I try to insert answer in the Answer table, it should check if Question.QID = Answers.QID. If true, Assign Answer.AID from 1 to 4 and enter the answer.</p>
  
  <p>All this is managed by Admin, so I have created a Web form AddQuestion.aspx where the question can be added when admin clicks the Add button and it redirects to AddAnswer.aspx where the admin can save and upload the answers.</p>
  
  <p>AddQuestion.aspx</p>
  
  <pre><code>public partial class AddQuestion : System.Web.UI.Page
  {
      SqlCommand command;
      SqlConnection connection;
      protected void ButtonAdd_Click(object sender, EventArgs e)
      {
          connection = new SqlConnection(ConfigurationManager.ConnectionStrings["TheQuizConnectionString"].ConnectionString);
          connection.Open();
  
          string addQuestion = "insert into Question(Question) values(@Ques)";
          command = new SqlCommand(addQuestion, connection);
  
          //command.Parameters.AddWithValue("@Qid", TextBoxQid.Text);
          command.Parameters.AddWithValue("@Ques", TextBoxQuestion.Text);
          command.ExecuteNonQuery();
  
          Response.Redirect("AddAnswer.aspx");
  
          connection.Close();
      }
  }
  </code></pre>
  
  <p>AddAnswer.aspx</p>
  
  <pre><code>public partial class AddAnswer : System.Web.UI.Page
  {
       SqlCommand command;
       SqlConnection connection;
       protected void ButtonAdd_Click(object sender, EventArgs e)
       {
  
          Response.Redirect("AddQuestion.aspx");
       }
       public void CheckQid()
      {
          connection = new SqlConnection(ConfigurationManager.ConnectionStrings["TheQuizConnectionString"].ConnectionString);
          connection.Open();
  
          string checkQid = "select count(QID) from Question";
          using (command = new SqlCommand(checkQid, connection))
  
      }
      protected void ButtonSave_Click(object sender, EventArgs e)
      {
          connection = new SqlConnection(ConfigurationManager.ConnectionStrings["TheQuizConnectionString"].ConnectionString);
          connection.Open();
  
          string addAnswer = "insert into Answers(Answer) values(@Answer)";
          command = new SqlCommand(addAnswer, connection);
  
          //command.Parameters.AddWithValue("@Aid", TextBoxAid.Text);
          //command.Parameters.AddWithValue("@Qid", TextBoxQid.Text);
          command.Parameters.AddWithValue("@Answer", TextBoxAnswer.Text);
  
          command.ExecuteNonQuery();
          Response.Redirect("AddAnswer.aspx");
      }
  }
  </code></pre>
  
  <p>I am new to asp.net and visual studio. I tried to search for answers but could not find anything. I appreciate if someone could help me out with this.</p>
  
  <p>Thanks.</p>