C# 如何从combobox中选择一个值并将其插入c中的记录中

C# 如何从combobox中选择一个值并将其插入c中的记录中,c#,select,combobox,insert,C#,Select,Combobox,Insert,你好,我是c的新手,我在组合框中遇到了一个问题 我希望用户从组合框中选择一个值,并使用所选数据的值在文本框中键入一些信息 所有数据都应保存在另一个表中 组合框由国籍表填充,文本框包含员工信息 该应用程序使用sql server数据库,表单使用c 谢谢 我想出的代码: 在此输入代码Private void btnSave\u Clickobject sender,EventArgs e { try { ExPForm(); s

你好,我是c的新手,我在组合框中遇到了一个问题

我希望用户从组合框中选择一个值,并使用所选数据的值在文本框中键入一些信息

所有数据都应保存在另一个表中

组合框由国籍表填充,文本框包含员工信息

该应用程序使用sql server数据库,表单使用c

谢谢 我想出的代码:

在此输入代码Private void btnSave\u Clickobject sender,EventArgs e

 {
        try
        {
            ExPForm();
       string strConnectionString = @"Data Source = C:\\SQLEXPRESS; Integrated Security = SSPI; Initial Catalog = DB_Admin";
            SqlConnection con = new SqlConnection(strConnectionString);
            con.Open();
            string strExpCode = txtExpCode.Text.Trim();
            DateTime dtExpDate = dtPickerExpDate.Value;
            string strExpEmp = txtExpEmp.Text.Trim();
            string strExpGov = comboExpGov.SelectedItem.ToString();
        string strExpSubTitle = txtExpSubTitle.Text.Trim();
            string strExpSubSum = richtxtExpSubSum.Text.Trim();
             string strExpSubNote = txtExpSubNote.Text.Trim();
            string query = "INSERT INTO ExpTbl(ExpCode, ExpDate, ExpEmpName, GovID, OutInID,SubTypeID, LocID, ExpSubTitle, ExpSubSum, ExpSubNote)VALUES(@ExpCode, @ExpDate, @ExpEmpName, @GovID, OutInID,SubTypeID, LocID, @ExpSubTitle, @ExpSubSum, @ExpSubNote)";
            SqlCommand InsertCommand = new SqlCommand(query, con);
            InsertCommand.Connection = con;
            InsertCommand.Parameters.AddWithValue(@"strExpCode ", strExpCode);
            InsertCommand.Parameters.AddWithValue(@"strExpDate ", strDate);
            InsertCommand.Parameters.AddWithValue(@"GovID", GovID);
            InsertCommand.Parameters.AddWithValue(@"strOutInID",2);
            InsertCommand.Parameters.AddWithValue(@"SubTypeID", 2);
            InsertCommand.Parameters.AddWithValue(@"LocID", 2);
        InsertCommand.Parameters.AddWithValue(@"strExpSubTitle ", strExpSubTitle);
        InsertCommand.Parameters.AddWithValue(@"strExpSubSum ", strExpSubSum);
        InsertCommand.Parameters.AddWithValue(@"strExpSubNote ", strExpSubNote);           
 InsertCommand.ExecuteNonQuery();
            MessageBox.Show("New record has been added successfully");
            con.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

我们是在家工作吗你试过什么了吗?你能粘贴代码吗。。无论你做了什么,我都做了选择部分,但插入部分我被卡住了,如何获取所选值并将其插入保存中button@MUAl-您的代码仍然有助于理解正在发生的事情。@MUAl-t您可以编辑您的问题,请。