executiontenonquery连接属性尚未初始化。在asp.net中 [英] executenonquery connection property has not been initialized. in asp.net
问题描述
代码 -
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();
连接字符串 -
< connectionstrings >
< add < span class =code-attribute> 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 >
您尚未设置命令的连接。将以下行添加到您的代码中
cmd.Connection = con;
或者在创建时定义连接命令
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);
作为旁注,命令看起来不正确。如果需要使用下拉列表中的值,则需要将它们定义为参数。
另一方面,Dept_Name,Dept_ID的值来自SELECT语句,因此不需要那些作为参数。
您似乎也与列数不匹配。 Insert定义了3列但select只提供了2
你需要指定在sql命令中使用哪个连接。
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);
并且您还没有在您的参数中包含参数sql语句,它应该如下所示
INSERT INTO 员工(Dept_ID,Dept_Name) SELECT Dept_ID,Dept_Name FROM 部门 WHERE Dept_Name = @ Dept_Name
因为你有一个参数,你可以设置如下
cmd.Parameters.AddWithValue( @ Dept_Name,ddlDeptName.SelectedValue);
Code-
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-
<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"/>
</add></connectionstrings>
You haven't set the connection for the command. Add the following line to your code
cmd.Connection = con;
Or define the connection when creating the command
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
you need to specify which connection to use in sql command.
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
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
cmd.Parameters.AddWithValue("@Dept_Name", ddlDeptName.SelectedValue);
这篇关于executiontenonquery连接属性尚未初始化。在asp.net中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!