提问者:小点点

根据ComboBox选择更改ComboBox值C#


我一直在研究和尝试所有不同的方法,试图让我所有的组合框的值根据每次选择新选择时第一个特定组合框中的值而改变。

下面是我使用的代码,但我一直在错误无效的语法附近')'

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;
            }
        }
    }
}

共2个答案

匿名用户

这意味着SQL查询中存在语法错误。

实际上,在代码的这一部分还有一个“'):(tblFleetSetups.FleetCat)

确切代码应为:

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+ "'));";
      

匿名用户

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命令,您需要在所有命令中执行此操作,是的,需要