Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Code-
C#
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
con.Open();
      
SqlCommand cmd= new SqlCommand("INSERT INTO Employee (Dept_Name, Dept_ID, Emp_Name) SELECT Dept_ID, Dept_Name FROM Department WHERE Dept_Name = ddlDeptName.SelectedValue ");
cmd.Parameters.AddWithValue("Emp_Name", txtName.Text.Trim());
cmd.Parameters.AddWithValue("Dept_Name", ddlDeptName.SelectedValue);
cmd.ExecuteNonQuery();
con.Close();


Connection String-

XML
<connectionstrings>
  <add name="constr">
       connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\users\rohit kumar\documents\visual studio 2013\Projects\Data Grid1\Data Grid1\App_Data\Employee.mdf;Integrated Security=True" 
       providerName="System.Data.SqlClient"/&gt;
</add></connectionstrings>
Posted
Updated 24-Aug-15 18:12pm
v2

You haven't set the connection for the command. Add the following line to your code
C#
cmd.Connection = con;

Or define the connection when creating the command
C#
SqlCommand cmd= new SqlCommand("INSERT INTO Employee (Dept_Name, Dept_ID, Emp_Name) SELECT Dept_ID, Dept_Name FROM Department WHERE Dept_Name = ddlDeptName.SelectedValue ", con);

As a side note the command doesn't look right. If you need to use values from a drop down list, you need to define them as parameters.

On the other hand, the values for Dept_Name, Dept_ID come from the SELECT statement so those aren't needed as parameters.

It also seems that you have a mismatch with column count. Insert defines 3 columns but the select provide only 2
 
Share this answer
 
v2
Comments
DamithSL 25-Aug-15 0:19am    
5wd!
Wendelius 25-Aug-15 0:23am    
Thanks :)
aarif moh shaikh 25-Aug-15 0:19am    
agree with you..
Rohitk2409 25-Aug-15 0:20am    
thank you :)
Wendelius 25-Aug-15 0:23am    
You're welcome, have a look at the updated answer. It seems there are some other issues with the statement....
you need to specify which connection to use in sql command.
C#
SqlCommand cmd= new SqlCommand("INSERT INTO Employee (Dept_Name, Dept_ID, Emp_Name) SELECT Dept_ID, Dept_Name FROM Department WHERE Dept_Name = ddlDeptName.SelectedValue ", con);

and also you haven't include parameters in your sql statement, it should be like below
SQL
INSERT INTO Employee (Dept_ID,Dept_Name) SELECT Dept_ID, Dept_Name FROM Department WHERE Dept_Name = @Dept_Name  

since you have one parameter, you can set it as below
C#
cmd.Parameters.AddWithValue("@Dept_Name", ddlDeptName.SelectedValue);
 
Share this answer
 
v3
Comments
Wendelius 25-Aug-15 0:24am    
Exactly, 5.
DamithSL 25-Aug-15 0:30am    
Thank you, Mika :)
Rohitk2409 25-Aug-15 0:40am    
it again showing me the error message, i tried updated code

Must declare the scalar variable "@Dept_Name".
DamithSL 25-Aug-15 0:41am    
what is the sql statement you have used?
Rohitk2409 25-Aug-15 0:45am    
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("Insert into Employee values(@Emp_Name)", con);
SqlCommand cmd1= new SqlCommand("INSERT INTO Employee (Dept_Name, Dept_ID) SELECT Dept_ID, Dept_Name FROM Department WHERE Dept_Name = @Dept_Name", con);
cmd1.Parameters.AddWithValue("Emp_Name", txtName.Text.Trim());
cmd1.Parameters.AddWithValue("Dept_Name", ddlDeptName.SelectedValue);
cmd.ExecuteNonQuery();
cmd1.ExecuteNonQuery();
con.Close();

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



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