如何从组合框中选择一个值并将其插入到c#中的记录中
你好我是新来的C#和我有一个组合框中的问题。
我希望用户从组合框中选择一个值并使用所选数据的值,在文本框中输入一些信息。
所有数据应保存在另一个表中。
组合框由国籍表填充,文本框包含员工信息。
该应用程序使用SQL Server数据库,表单位于c#中。
谢谢
我提出的代码::::::::::
enter code here
private void btnSave_Click(object 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);
}
}
链接地址: http://www.djcxy.com/p/68599.html
上一篇: how to choose a value from combobox and insert it into record in c#