【发布时间】: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 注入攻击。你应该解决这个问题。
标签: c# wpf visual-studio