且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

数据未保存在数据库中,没有错误。

更新时间:2023-02-15 11:27:52

试试这个。

Try this.
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();

        }
    }
}


cmd.Parameters.AddWithValue("@Dept_Name", ddlDeptName.SelectedItem.Text);


试试这个..



Try This..

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

}