C# 使用组合框选择来预填充文本框

本文关键字:填充 文本 选择 组合 | 更新日期: 2023-09-27 18:36:14

我是一个非常菜鸟程序员,试图编写一点软件。

在此代码中,我尝试在新选择 ComboBox 时以包含 OleDB 数据库详细信息的形式预填充文本框。

基本上,单击组合框中的名称将预先填充一些文本框,其中包含有关该名称的详细信息。

问题是代码不会将数据中的值分配到文本框中。而是文本框的显示"0"。

我该如何解决这个问题?

private void cbbEmployees_SelectedIndexChanged(object sender, EventArgs e)
    {
        int PayrollNo = 0;
        int AnnualHolidayEntitlemet = 0;
        int DaysTakenToDate = 0;
        string Query = "SELECT PayrollNo, AnnualHolidayEntitlement, DaysTakenToDate FROM [Employee] WHERE FirstName +  ' ' + LastName = ?";
        string ConnString = @"Provider = Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|'HoliPlanData.accdb;Persist Security Info=False";
        using (OleDbConnection conn = new OleDbConnection(ConnString))
        using (OleDbCommand GetAllcmd = new OleDbCommand(Query, conn))
        {
            conn.Open();
            GetAllcmd.Parameters.Add("?", OleDbType.VarChar).Value = cbbEmployees.Text;
            GetAllcmd.Parameters.Add("@PayrollNo", OleDbType.VarChar).Value = PayrollNo;               
            GetAllcmd.Parameters.Add("@AnnualHolidayEntitlement", OleDbType.VarChar).Value = AnnualHolidayEntitlemet;
            GetAllcmd.Parameters.Add("@DaysTakenToDate", OleDbType.VarChar).Value = DaysTakenToDate;
            GetAllcmd.ExecuteScalar();

            txtPayrollNo.Text = PayrollNo.ToString();
            txtAHE.Text = AnnualHolidayEntitlemet.ToString();
            txtDTTD.Text = DaysTakenToDate.ToString();
            txtDaysRemaining.Text = (AnnualHolidayEntitlemet - DaysTakenToDate).ToString();
        }
    }

C# 使用组合框选择来预填充文本框

https://stackoverflow.com/users/5292801/takarii

感谢takarii ^^.的广泛指导,以下是该问题的工作解决方案!

    private void cbbEmployees_SelectionChangeCommitted(object sender, EventArgs e)
    {
        string Query = "SELECT PayrollNo, AnnualHolidayEntitlement, DaysTakenToDate FROM [Employee] WHERE PayrollNo =" + Convert.ToInt32(cbbEmployees.SelectedValue); 

        string ConnString = @"Provider = Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|'HoliPlanData.accdb;Persist Security Info=False";
        using (OleDbConnection conn = new OleDbConnection(ConnString))
        using (OleDbCommand GetAllcmd = new OleDbCommand(Query, conn))
        {
            DataTable dt = new DataTable();
            conn.Open();
            dt.Load(GetAllcmd.ExecuteReader());
            conn.Close();
             txtPayrollNo.Text = dt.Rows[0][0].ToString();
             txtAHE.Text = dt.Rows[0][1].ToString();
             txtDTTD.Text = dt.Rows[0][2].ToString();

        }
    }

我的代码示例使用找到的第一个记录的数据。

您可以使用选择前 1 个...来调整查询。以确保您拥有正确的记录。

如果你的结果集可以包含多个记录,你必须在一段时间内处理它(oleDR.Read()) { 做点什么 }

private void cbbEmployees_SelectedIndexChanged(object sender, EventArgs e)
    {
        int PayrollNo = 0;
        int AnnualHolidayEntitlemet = 0;
        int DaysTakenToDate = 0;
        string Query = "SELECT TOP(1) PayrollNo, AnnualHolidayEntitlement, DaysTakenToDate FROM [Employee] WHERE FirstName" +  "' '" + "LastName = @param";
        string ConnString = @"Provider = Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|'HoliPlanData.accdb;Persist Security Info=False";
        using (OleDbConnection conn = new OleDbConnection(ConnString))
        using (OleDbCommand GetAllcmd = new OleDbCommand(Query, conn))
        {
            conn.Open();
            GetAllcmd.Parameters.AddWithValue("@param", cbbEmployees.Text);

            //GetAllcmd.ExecuteScalar(); use GetAllcmd.ExecuteReader() instead
            OleDbDataReader oleDR;
                    oleDR = GetAllcmd.ExecuteReader();
                    if (oleDR.HasRows)
                    {
                         txtPayrollNo.Text = oleDR["PayrollNo"].ToString();
                         txtAHE.Text = oleDR["AnnualHolidayEntitlemet"].ToString();
                         txtDTTD.Text = oleDR["DaysTakenToDate"].ToString();
                         txtDaysRemaining.Text = (oleDR["AnnualHolidayEntitlemet"] - oleDR["DaysTakenToDate"]).ToString();
                    }

        }
    }