Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
1st table

SQL
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

SQL
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();

}
}
}
Posted
Comments
Sarath kumar.N 25-Aug-15 2:51am    
What SQL query actually you have written?
Rohitk2409 25-Aug-15 2:55am    
Insert into Employee (Dept_ID, Dept_Name, Emp_Name) Select Dept_ID, Dept_Name, @Emp_Name from Department WHERE Dept_Name = @Dept_Name
Sarath kumar.N 25-Aug-15 3:06am    
Actually you have written a query for inserting multiple records right?
Rohitk2409 25-Aug-15 3:37am    
yes, i want to insert data in employee table but data is not inserting in table
Sarath kumar.N 25-Aug-15 3:39am    
Did u tried below solution?

Try this.
C#
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();
            string Dept_Name = ddlDeptName.SelectedItem.Text;
            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.ExecuteNonQuery();
            con.Close();

        }
    }
}
 
Share this answer
 
v2
Comments
Rohitk2409 25-Aug-15 3:42am    
still not saved
Sarath kumar.N 25-Aug-15 3:48am    
Please explain ur scenario that will help others. If u want insert multiple rows means u can use sqlbulkcopy. actually ur code is not giving any clue. why u r not using simple query instead of this query?
Rohitk2409 25-Aug-15 3:52am    
1st i entered data in Department table(Dept_ID, Dept_Name), here Dept_ID is the FK for table Employee of column Dept_ID. Now I'm entering data in Employee table(Dept_ID, Dept_Name, Emp_ID(auto-incremented), Emp_Name). I want to enter the Emp_Name in that dept_name which i will select from dropdownlist. with corresponding Dept_ID.
cmd.Parameters.AddWithValue("@Dept_Name", ddlDeptName.SelectedItem.Text);
 
Share this answer
 
Comments
Rohitk2409 25-Aug-15 3:45am    
nothing happened, data is still not saved
Rohitk2409 25-Aug-15 4:07am    
thank You data is now saved
Mayank Vashishtha 25-Aug-15 4:17am    
If it is solved. Please Mark it as Solved.
Rohitk2409 25-Aug-15 4:25am    
how will I mark that it is solved?
Arasappan 25-Aug-15 5:08am    
Keep on trying Rohitk
Try This..

C#
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,'" + txtName.Text.Trim() + "' from Department WHERE Dept_Name = '"+ ddlDeptName.SelectedItem.Value +"'", con);

cmd.ExecuteNonQuery();
con.Close();

}
 
Share this answer
 
Comments
Rohitk2409 25-Aug-15 3:59am    
same thing happened :(
WHERE Dept_Name = @Dept_Name


This is returning no matches, so there is nothing in Dept_Name that matches what is in your @Dept_Name param. We don't know what is in your Dept_Name param and we don't know what is in your data, so apart from telling you this there is little more we can do. Either what you think is in your variable isn't, or what you think is in your data isn't, you have to examine your variables and your data and try and work it out.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900