根据组合框选择 C# 更改组合框值
Posted
技术标签:
【中文标题】根据组合框选择 C# 更改组合框值【英文标题】:Changing ComboBox Values according to a ComboBox Selection C# 【发布时间】:2022-01-23 18:37:19 【问题描述】:我一直在研究并尝试所有不同的方法来尝试让我的所有组合框的值在每次选择新选择时根据第一个特定组合框中的值进行更改。
以下是我使用过的代码,但在 ')' 附近不断出现错误语法无效
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Configuration;
using MaterialDesignColors;
using MaterialDesignThemes;
using System.Management;
using System.Windows.Forms;
using System.Data;
using ToolTip = System.Windows.Controls.ToolTip;
namespace SolAquaPro
public partial class FuelEntry : Form
readonly SqlConnection conn1 = new SqlConnection();
readonly SqlCommand cmd1 = new SqlCommand();
SqlDataReader dr1;
readonly SqlConnection conn3 = new SqlConnection();
readonly SqlCommand cmd3 = new SqlCommand();
SqlDataReader dr3;
readonly SqlConnection conn6 = new SqlConnection();
readonly SqlCommand cmd6 = new SqlCommand();
SqlDataReader dr6;
public FuelEntry()
InitializeComponent();
conn1.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString.ToString();
conn3.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString.ToString();
conn6.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString.ToString();
private void BtnClose_Click(object sender, EventArgs e)
this.Close();
Form frmFuelDash = new FuelDash();
frmFuelDash.Show();
private SqlDataReader GetDr1()
return dr1;
private void FuelEntry_Load(object sender, EventArgs e)
conn1.Open();
conn3.Open();
cmd1.Connection = conn1;
cmd1.CommandText = @"SELECT FleetNo, Driver, Registration, Company, Category, Target, Unit, FuelAcc, FuelRef,
FuelDepot FROM tblFleetSetups WHERE (((tblFleetSetups.FleetCat)<>'Trailer' And
(tblFleetSetups.FleetCat)<>'Retired'));";
dr1 = cmd1.ExecuteReader();
// TODO: This line of code loads data into the 'solAquaProjectMasterDataDataSet4.tblFuelDepots' table. You can move, or remove it, as needed.
this.tblFuelDepotsTableAdapter.Fill(this.solAquaProjectMasterDataDataSet4.tblFuelDepots);
// TODO: This line of code loads data into the 'solAquaProjectMasterDataDataSet3.tblFuelAccs' table. You can move, or remove it, as needed.
this.tblFuelAccsTableAdapter.Fill(this.solAquaProjectMasterDataDataSet3.tblFuelAccs);
// TODO: This line of code loads data into the 'solAquaProjectMasterDataDataSet2.tblCompanies' table. You can move, or remove it, as needed.
this.tblCompaniesTableAdapter.Fill(this.solAquaProjectMasterDataDataSet2.tblCompanies);
// TODO: This line of code loads data into the 'solAquaProjectMasterDataDataSet1.tblDrivers' table. You can move, or remove it, as needed.
this.tblDriversTableAdapter.Fill(this.solAquaProjectMasterDataDataSet1.tblDrivers);
// TODO: This line of code loads data into the 'solAquaProjectMasterDataDataSet.tblFleetSetups' table. You can move, or remove it, as needed.
this.tblFleetSetupsTableAdapter.Fill(this.solAquaProjectMasterDataDataSet.tblFleetSetups);
cboCompany.SelectedIndex = -1;
cboDepot.SelectedIndex = -1;
cboDriver.SelectedIndex = -1;
cboFuelAcc.SelectedIndex = -1;
cboFleetNo.SelectedIndex = -1;
txtCategory.Text = "";
txtFuelRef.Text = "";
txtReg.Text = "";
txtTarget.Text = "";
txtUnit.Text = "";
private void BtnAdd_MouseHover(object sender, EventArgs e)
ToolTip tip = new ToolTip();
tip.Content = "Add Fuel Entry";
private void BtnUndo_MouseHover(object sender, EventArgs e)
ToolTip tip = new ToolTip();
tip.Content = "Reset To Blank Entry";
private void BtnProcess_MouseHover(object sender, EventArgs e)
ToolTip tip = new ToolTip();
tip.Content = "Go To Fuel Entry Processing";
private void BtnClose_MouseHover(object sender, EventArgs e)
ToolTip tip = new ToolTip();
tip.Content = "Close Form";
private void CboFleetNo_TextChanged(object sender, EventArgs e)
if (cboFleetNo.SelectedIndex == -1)
cboCompany.SelectedIndex = -1;
cboDepot.SelectedIndex = -1;
cboDriver.SelectedIndex = -1;
cboFuelAcc.SelectedIndex = -1;
txtCategory.Text = "";
txtFuelRef.Text = "";
txtReg.Text = "";
txtTarget.Text = "";
txtUnit.Text = "";
else if ((cboFleetNo.SelectedIndex != -1) && (isMatch(cboFleetNo.Text.ToString())))
txtReg.Text = dr1.GetValue(2).ToString();
txtTarget.Text = dr1.GetValue(5).ToString();
txtUnit.Text = dr1.GetValue(6).ToString();
cboCompany.Text = dr1.GetValue(3).ToString();
cboDepot.Text = dr1.GetValue(9).ToString();
cboDriver.Text = dr1.GetValue(1).ToString();
cboFuelAcc.Text = dr1.GetValue(7).ToString();
txtCategory.Text = dr1.GetValue(4).ToString();
conn6.Close();
private void CboFuelAcc_TextChanged(object sender, EventArgs e)
if (cboFuelAcc.SelectedIndex == -1)
txtFuelRef.Text = "";
else if (cboFuelAcc.SelectedIndex != -1)
string FuelAcc = cboFuelAcc.Text.ToString();
conn3.Close();
conn3.Open();
cmd3.Connection = conn3;
cmd3.CommandText = @"SELECT [FuelAcc] ,[FuelRef]
FROM [dbo].[tblFuelAccs]
WHERE FuelAcc = '" + FuelAcc + "';";
dr3 = cmd3.ExecuteReader();
dr3.Read();
txtFuelRef.Text = dr3.GetValue(1).ToString();
conn3.Close();
private bool isMatch(string iMatch)
iMatch = cboFleetNo.Text.ToString();
conn6.Close();
conn6.Open();
cmd6.Connection = conn6;
cmd6.CommandText = @"SELECT 1 FleetNo, Driver, Registration, Company, Category, Target, Unit, FuelAcc, FuelRef,
FuelDepot FROM tblFleetSetups WHERE (((tblFleetSetups.FleetCat)<>'Trailer' And
(tblFleetSetups.FleetCat)<>'Retired') And (tblFleetSetups.FleetNo)= '" +@iMatch+ "'));";
dr6 = cmd6.ExecuteReader();
if (dr6.Read())
if (Convert.ToBoolean(dr6["FleetNo"]) == true)
return true;
else
return false;
else
return false;
我们将不胜感激。
【问题讨论】:
什么,在哪里,什么时候?请提供有关错误的更多详细信息。是编译错误还是运行时错误?是在 C#、XAML、SQL 中吗? 您的代码容易受到 SQL 注入攻击。你应该解决这个问题。 【参考方案1】:这意味着您的 SQL 查询存在语法错误。
其实这部分代码多了一个')':(tblFleetSetups.FleetCat)<>'Retired')
.
确切的代码应该是:
cmd6.CommandText = @"SELECT 1 FleetNo, Driver, Registration, Company, Category, Target, Unit, FuelAcc, FuelRef,
FuelDepot FROM tblFleetSetups WHERE (((tblFleetSetups.FleetCat)<>'Trailer' And
(tblFleetSetups.FleetCat)<>'Retired' And (tblFleetSetups.FleetNo)= '" +@iMatch+ "'));";
【讨论】:
所以我已根据您的更正进行了更正,但它不断抛出以下错误: System.InvalidOperationException: '当没有数据存在时读取无效尝试。在行 txtReg.Text = dr1.GetValue(2).ToString();【参考方案2】:cmd6.CommandText 似乎完全错误,错误似乎出在 SQL 部分,所以....试试:
cmd6.Parameters.Add(new SqlParameter("iMatch", iMatch));
cmd6.CommandText = @"SELECT 1 FleetNo, Driver, Registration, Company, Category, Target, Unit, FuelAcc, FuelRef,
FuelDepot FROM tblFleetSetups WHERE (((tblFleetSetups.FleetCat)<>'Trailer' And
(tblFleetSetups.FleetCat)<>'Retired') And (tblFleetSetups.FleetNo)= @iMatch);";
注意参数化的 SQL 命令,您需要在所有命令中执行此操作,是的,需要
【讨论】:
所以我已根据您的更正进行了更正,但它不断抛出以下错误: System.InvalidOperationException: '在不存在数据时尝试读取无效。'在行 txtReg.Text = dr1.GetValue(2).ToString(); 作为错误点,此选择不产生任何数据 SELECT FleetNo, Driver, Registration, Company, Category, Target, Unit, FuelAcc, FuelRef, FuelDepot FROM tblFleetSetups WHERE (((tblFleetSetups.FleetCat) '预告片'和(tblFleetSetups.FleetCat)'退休'));需要在 GetValue 之前检查 dr1(和所有 drx)中的数据 或者当 CboFleetNo_TextChanged 第一次运行时加载没有完成获取数据请参阅 aspx 页面的生命周期docs.microsoft.com/en-us/previous-versions/aspnet/…以上是关于根据组合框选择 C# 更改组合框值的主要内容,如果未能解决你的问题,请参考以下文章