1st table
CREATE TABLE [dbo].[Department] (
[Dept_ID] INT IDENTITY (10, 10) NOT NULL,
[Dept_Name] VARCHAR (50) NOT NULL,
CONSTRAINT [PK_Department] PRIMARY KEY CLUSTERED ([Dept_ID] ASC),
CONSTRAINT [FK_Department_Employee] FOREIGN KEY ([Dept_ID]) REFERENCES [dbo].[Department] ([Dept_ID])
);
2nd table
CREATE TABLE [dbo].[Employee] (
[Dept_ID] INT NOT NULL,
[Dept_Name] VARCHAR (50) NOT NULL,
[Emp_ID] INT IDENTITY (1, 1) NOT NULL,
[Emp_Name] VARCHAR (50) NOT NULL,
CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED ([Emp_ID] ASC)
);
code-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace Data_Grid1
{
public partial class Employee : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("Insert into Employee (Dept_ID, Dept_Name, Emp_Name) Select Dept_ID, Dept_Name, @Emp_Name from Department WHERE Dept_Name = @Dept_Name", con);
cmd.Parameters.AddWithValue("@Emp_Name", txtName.Text.Trim());
cmd.Parameters.AddWithValue("@Dept_Name", ddlDeptName.SelectedValue);
cmd.ExecuteNonQuery();
con.Close();
}
}
}