title: fill foreign key from primary key using asp and C#
body: <p>I am writing a course and package form in asp.net using c# as I am new I am facing problem when executing my task is to insert the foreign key of that selected item in database so I make three tables <code>courses</code> which contains <code>Id</code> (pk) and <code>Course-name</code>, <code>Pkg-code</code> contain <code>Id</code> (pk), <code>Pkg-name</code> and last table is <code>Pkg</code> which contains <code>Pkg-id</code> (fk) referencing the <code>Pkg-Code(Id)</code> and <code>Course-Id</code> (fk) referencing <code>Courses(Id)</code>.</p>
  
  <p>Now what I have to do is when I select the package from drop down and course from check box in web app the Id's of that selected item will insert in the third table <code>Pkg</code> via foreign key now the problem I am facing is when I execute it error is generating cannot insert NULL values in <code>Course-Id</code>, my stored procedure is working fine when executing it the values inserted in the third table pkg and I want to make one to many relation in database, I have searched lot but cannot find required answer please help </p>
  
  <pre><code>&lt;div&gt;
    Select Pkg:  &lt;asp:DropDownList ID ="drop1" runat="server"&gt;
                 &lt;/asp:DropDownList&gt;
  &lt;/div&gt;
  &lt;div style="text-align:center"&gt;
      &lt;asp:CheckBoxList ID ="check1" runat="server"&gt;
      &lt;/asp:CheckBoxList&gt;
  &lt;/div&gt;
  &lt;div&gt;
      &lt;asp:Button ID="Button1" runat="server" Text="Save" OnClick="Button1_Click" /&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
      string constr = ConfigurationManager.ConnectionStrings["Db"].ConnectionString;
  
      SqlConnection conn = new SqlConnection(constr);
      conn.Open();
  
      SqlCommand cmd = new SqlCommand("Select * from PKgCode ", conn);
      SqlDataAdapter data = new SqlDataAdapter(cmd);
  
      DataSet da = new DataSet();
  
      data.Fill(da);
  
      drop1.DataSource = da;
  
      //drop1.DataValueField = "PkgName";
      drop1.DataTextField = "PkgName";
      drop1.DataValueField="Id";
  
      drop1.DataBind();
  
      conn.Close();
  
      conn.Open();
  
      SqlCommand cmdd = new SqlCommand("Select * from Courses ", conn);
  
      SqlDataAdapter daa = new SqlDataAdapter(cmdd);
  
      DataSet ss = new DataSet();
  
      daa.Fill(ss);
  
      check1.DataSource = ss;
      check1.DataTextField = "CourseName";
      check1.DataValueField = "Id";
      check1.DataBind();
  
      conn.Close();
  }
  
  protected void Button1_Click(object sender, EventArgs e)
  {
       string constr = ConfigurationManager.ConnectionStrings["Db"].ConnectionString;
       SqlConnection con = new SqlConnection(constr);
  
       SqlCommand cmm = new SqlCommand("Pkc", con);
       //SqlCommand cm = new SqlCommand("PkI", con);
       cmm.CommandType = CommandType.StoredProcedure;
       //  cm.CommandType = CommandType.StoredProcedure;
       cmm.Parameters.Add("@pname", SqlDbType.NVarChar, 50).Value = drop1.SelectedValue;
       cmm.Parameters.Add("@course", SqlDbType.NVarChar, 50).Value = check1.SelectedValue;
  
       con.Open();
       cmm.ExecuteNonQuery();
       con.Close();
  }
  </code></pre>