Home > Net >  Changing ComboBox Values according to a ComboBox Selection C#
Changing ComboBox Values according to a ComboBox Selection C#

Time:12-22

I have been researching and trying all different methods to try and get the values of all my comboboxes to change according to the value in the first specific combox everytime a new selection is chosen.

Below is the code I have used but I keep getting the error invalid syntax near ')'

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

Any and all help would be much appreciated.

CodePudding user response:

That means there is a syntax error on your SQL query.

Actually there is an additional ')' at this part of the code: (tblFleetSetups.FleetCat)<>'Retired').

Exact code should be:

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

CodePudding user response:

cmd6.CommandText seems completly wrong, and the error seems to be on the SQL part, so.... TRY:

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

Note the parametrized SQL command, you need to do this in all your comands, yes NEED TO

  • Related