C# 使用组合框选择来预填充文本框的
C# Using ComboBox selection to PrePopulate TextBox's
我是一个非常菜鸟的程序员,正在尝试编写一小部分软件。
在这段代码中,我尝试在新选择 ComboBox 时以包含来自 OleDB 数据库的详细信息的形式预填充文本框。
基本上,单击 ComboBox 中的名称会预先填充一些文本框,其中包含有关该名称的详细信息。
问题是代码没有将数据中的值赋给文本框。取而代之的是文本框的显示“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();
}
}
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();
}
}
}
https://whosebug.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();
}
}
我的代码示例使用第一个找到的记录的数据。
您可以使用 select Top 1 ....调整您的查询。
确保您有正确的记录。
如果您的结果集可能包含多个记录,您需要稍后处理 (oleDR.Read())
{
// 做一点事
}
我是一个非常菜鸟的程序员,正在尝试编写一小部分软件。
在这段代码中,我尝试在新选择 ComboBox 时以包含来自 OleDB 数据库的详细信息的形式预填充文本框。
基本上,单击 ComboBox 中的名称会预先填充一些文本框,其中包含有关该名称的详细信息。
问题是代码没有将数据中的值赋给文本框。取而代之的是文本框的显示“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();
}
}
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();
}
}
}
https://whosebug.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();
}
}
我的代码示例使用第一个找到的记录的数据。
您可以使用 select Top 1 ....调整您的查询。 确保您有正确的记录。
如果您的结果集可能包含多个记录,您需要稍后处理 (oleDR.Read()) { // 做一点事 }