C# oString(); } 十进制解析值2; if(!decimal.TryParse(fueliterstextbox.Text,out parsedValue2)) { } 其他的 { fuelGallonsTextBox.Text=(Convert.ToDecimal(fueliterstextbox.Text)/Convert.ToDecimal(convTextBox.Text)).ToString(); } 十进制解析值3; if(!Decimal.TryParse(fuelGallonsTextBox.Text,out parsedValue3)) { } 其他的 { fuelMPGTextBox.Text=Math.Round((Convert.ToDecimal(mileageMinusTextBox.Text)/Convert.ToDecimal(fuelGallonsTextBox.Text)),3).ToString(); } fuelFromTextBox.Refresh(); fuelToTextBox.Refresh(); fueliterstextbox.Refresh(); fuelGallonsTextBox.Refresh(); fuelMPGTextBox.Refresh(); mileageMinusTextBox.Refresh(); } 私有void fuelGallonsTextBox_TextChanged(对象发送方,事件参数e) { 十进制解析值; if(!Decimal.TryParse(fuelGallonsTextBox.Text,out parsedValue)) { } 其他的 { fuelMPGTextBox.Text=Math.Round((Convert.ToDecimal(mileageMinusTextBox.Text)/Convert.ToDecimal(fuelGallonsTextBox.Text)),3).ToString(); } fuelFromTextBox.Refresh(); fuelToTextBox.Refresh(); fueliterstextbox.Refresh(); fuelGallonsTextBox.Refresh(); fuelMPGTextBox.Refresh(); mileageMinusTextBox.Refresh(); } 私有void fuelRegView\u SelectedIndexChanged(对象发送方,事件参数e) { BindingSource bs=新的BindingSource(); bs.DataSource=fuelDataGridView.DataSource; bs.Filter=“类似fuelreg的“%”+fuelRegView.Text+“%”; fuelDataGridView.DataSource=bs; } 私有无效选项卡控件1\u单击(对象发送方,事件参数e) { BindingSource bs=新的BindingSource(); bs.DataSource=fuelDataGridView.DataSource; bs.Filter=“像“%”一样的fuelreg”; fuelDataGridView.DataSource=bs; fuelRegView.SelectedIndex=-1; } 私有void fuelFromTextBox\u TextChanged(对象发送者,事件参数e) { 十进制解析值; if(!decimal.TryParse(fuelToTextBox.Text,out parsedValue)) { } 其他的 { mileageMinusTextBox.Text=(Convert.ToDecimal(fuelToTextBox.Text)-Convert.ToDecimal(fuelFromTextBox.Text)).ToString(); } 十进制解析值2; if(!decimal.TryParse(fueliterstextbox.Text,out parsedValue2)) { } 其他的 { fuelGallonsTextBox.Text=(Convert.ToDecimal(fueliterstextbox.Text)/Convert.ToDecimal(convTextBox.Text)).ToString(); } 十进制解析值3; if(!Decimal.TryParse(fuelGallonsTextBox.Text,out parsedValue3)) { } 其他的 { fuelMPGTextBox.Text=Math.Round((Convert.ToDecimal(mileageMinusTextBox.Text)/Convert.ToDecimal(fuelGallonsTextBox.Text)),3).ToString(); } fuelFromTextBox.Refresh(); fuelToTextBox.Refresh(); fueliterstextbox.Refresh(); fuelGallonsTextBox.Refresh(); fuelMPGTextBox.Refresh(); mileageMinusTextBox.Refresh(); } } }
重写了所有的C# oString(); } 十进制解析值2; if(!decimal.TryParse(fueliterstextbox.Text,out parsedValue2)) { } 其他的 { fuelGallonsTextBox.Text=(Convert.ToDecimal(fueliterstextbox.Text)/Convert.ToDecimal(convTextBox.Text)).ToString(); } 十进制解析值3; if(!Decimal.TryParse(fuelGallonsTextBox.Text,out parsedValue3)) { } 其他的 { fuelMPGTextBox.Text=Math.Round((Convert.ToDecimal(mileageMinusTextBox.Text)/Convert.ToDecimal(fuelGallonsTextBox.Text)),3).ToString(); } fuelFromTextBox.Refresh(); fuelToTextBox.Refresh(); fueliterstextbox.Refresh(); fuelGallonsTextBox.Refresh(); fuelMPGTextBox.Refresh(); mileageMinusTextBox.Refresh(); } 私有void fuelGallonsTextBox_TextChanged(对象发送方,事件参数e) { 十进制解析值; if(!Decimal.TryParse(fuelGallonsTextBox.Text,out parsedValue)) { } 其他的 { fuelMPGTextBox.Text=Math.Round((Convert.ToDecimal(mileageMinusTextBox.Text)/Convert.ToDecimal(fuelGallonsTextBox.Text)),3).ToString(); } fuelFromTextBox.Refresh(); fuelToTextBox.Refresh(); fueliterstextbox.Refresh(); fuelGallonsTextBox.Refresh(); fuelMPGTextBox.Refresh(); mileageMinusTextBox.Refresh(); } 私有void fuelRegView\u SelectedIndexChanged(对象发送方,事件参数e) { BindingSource bs=新的BindingSource(); bs.DataSource=fuelDataGridView.DataSource; bs.Filter=“类似fuelreg的“%”+fuelRegView.Text+“%”; fuelDataGridView.DataSource=bs; } 私有无效选项卡控件1\u单击(对象发送方,事件参数e) { BindingSource bs=新的BindingSource(); bs.DataSource=fuelDataGridView.DataSource; bs.Filter=“像“%”一样的fuelreg”; fuelDataGridView.DataSource=bs; fuelRegView.SelectedIndex=-1; } 私有void fuelFromTextBox\u TextChanged(对象发送者,事件参数e) { 十进制解析值; if(!decimal.TryParse(fuelToTextBox.Text,out parsedValue)) { } 其他的 { mileageMinusTextBox.Text=(Convert.ToDecimal(fuelToTextBox.Text)-Convert.ToDecimal(fuelFromTextBox.Text)).ToString(); } 十进制解析值2; if(!decimal.TryParse(fueliterstextbox.Text,out parsedValue2)) { } 其他的 { fuelGallonsTextBox.Text=(Convert.ToDecimal(fueliterstextbox.Text)/Convert.ToDecimal(convTextBox.Text)).ToString(); } 十进制解析值3; if(!Decimal.TryParse(fuelGallonsTextBox.Text,out parsedValue3)) { } 其他的 { fuelMPGTextBox.Text=Math.Round((Convert.ToDecimal(mileageMinusTextBox.Text)/Convert.ToDecimal(fuelGallonsTextBox.Text)),3).ToString(); } fuelFromTextBox.Refresh(); fuelToTextBox.Refresh(); fueliterstextbox.Refresh(); fuelGallonsTextBox.Refresh(); fuelMPGTextBox.Refresh(); mileageMinusTextBox.Refresh(); } } },c#,C#,重写了所有的转换,以使用TryParse private void fuelFromTextBox_TextChanged(object sender, EventArgs e) { double from, to; if ( !double.TryParse(fuelFromTextBox.Text, out from) || !double.TryParse(fuelToTextBox.Text, out
转换,以使用TryParse
private void fuelFromTextBox_TextChanged(object sender, EventArgs e)
{
double from, to;
if (
!double.TryParse(fuelFromTextBox.Text, out from) ||
!double.TryParse(fuelToTextBox.Text, out to)
)
{
return;
}
mileageMinusTextBox.Text = (to - from).ToString();
decimal mile, gall;
if (
!decimal.TryParse(mileageMinusTextBox.Text, out mile) ||
!decimal.TryParse(fuelGallonsTextBox.Text, out gall)
)
{
return;
}
fuelMPGTextBox.Text = Math.Round((mile / gall), 3).ToString();
decimal litres, conv;
if (
!decimal.TryParse(fuelLitersTextBox.Text, out litres) ||
!decimal.TryParse(convTextBox.Text, out conv)
)
{
return;
}
fuelGallonsTextBox.Text = (litres / conv).ToString();
fuelFromTextBox.Refresh();
fuelToTextBox.Refresh();
fuelLitersTextBox.Refresh();
fuelGallonsTextBox.Refresh();
fuelMPGTextBox.Refresh();
mileageMinusTextBox.Refresh();
fuelFromTextBox.Update();
fuelToTextBox.Update();
fuelLitersTextBox.Update();
fuelGallonsTextBox.Update();
fuelMPGTextBox.Update();
mileageMinusTextBox.Update();
}
您从何处获得此错误?您的查询易受SQL注入攻击。考虑使用<代码> MyErgEnEuthStutoBox。文本< /COD>或<代码> FuelGalOnStestBox。文本< /代码>包含不可转换的字符串,因此<代码>转换。ToDecimal < /COD>投掷。您是否阅读了异常的堆栈跟踪(它确切地告诉您它被抛出的位置)?是吗?为什么您实际上调用TryParse
,然后调用Convert.ToDecimal
而不是使用parsedValue
?这是很少见的,但是当尝试转换来自具有不同cultureinfo
的用户的输入时,也会抛出此错误消息,因此您可以使用Convert.ToDecimal(字符串, IFormatProvider)
。
private void fuelFromTextBox_TextChanged(object sender, EventArgs e)
{
decimal parsedValue;
if (!decimal.TryParse(fuelToTextBox.Text, out parsedValue))
{
}
else
{
mileageMinusTextBox.Text = (Convert.ToDecimal(fuelToTextBox.Text) - Convert.ToDecimal(fuelFromTextBox.Text)).ToString();
}
decimal parsedValue2;
if (!decimal.TryParse(fuelLitersTextBox.Text, out parsedValue2))
{
}
else
{
fuelGallonsTextBox.Text = (Convert.ToDecimal(fuelLitersTextBox.Text) / Convert.ToDecimal(convTextBox.Text)).ToString();
}
Decimal parsedValue3;
if (!Decimal.TryParse(fuelGallonsTextBox.Text, out parsedValue3))
{
}
else
{
fuelMPGTextBox.Text = Math.Round((Convert.ToDecimal(mileageMinusTextBox.Text) / Convert.ToDecimal(fuelGallonsTextBox.Text)), 3).ToString();
}
fuelFromTextBox.Refresh();
fuelToTextBox.Refresh();
fuelLitersTextBox.Refresh();
fuelGallonsTextBox.Refresh();
fuelMPGTextBox.Refresh();
mileageMinusTextBox.Refresh();
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CTMS
{
public partial class Fuel : Form
{
public Fuel()
{
InitializeComponent();
}
private void Fuel_Load(object sender, EventArgs e)
{
this.fuelTableAdapter.Fill(this.cTDBDataSet.fuel);
SqlConnection con = new SqlConnection(Properties.Settings.Default.CTDBCS);
convTextBox.Text = "4.546";
con.Open();
if (con.State == ConnectionState.Open)
{
connectedPic.Visible = true;
disconnectedPic.Visible = false;
panel1.BackColor = Color.LightGreen;
}
else
{
connectedPic.Visible = false;
disconnectedPic.Visible = true;
panel1.BackColor = Color.OrangeRed;
}
string Sql = "SELECT DISTINCT reg FROM vehicles";
SqlCommand cmd = new SqlCommand(Sql, con);
SqlDataReader DR = cmd.ExecuteReader();
while (DR.Read())
{
fuelReg.Items.Add(DR[0]);
fuelReg.SelectedIndex = -1;
fuelRegView.Items.Add(DR[0]);
fuelRegView.SelectedIndex = -1;
}
con.Close();
mileageMinusTextBox.Text = "0";
}
private void AddFuelButton_Click(object sender, EventArgs e)
{
fuelDateTimeCombined.Text = fuelDatePicker.Text + ' ' + fuelTimePicker.Text;
int currmile;
int prevmile;
Int32.TryParse(fuelToTextBox.Text, out currmile);
Int32.TryParse(fuelFromTextBox.Text, out prevmile);
if (string.IsNullOrWhiteSpace(fuelReg.Text) || string.IsNullOrWhiteSpace(fuelFromTextBox.Text) || string.IsNullOrWhiteSpace(fuelLitersTextBox.Text) ||
currmile < prevmile)
{
MessageBox.Show("Values Missing or Mileage less than previous", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
SqlConnection con = new SqlConnection(Properties.Settings.Default.CTDBCS);
con.Open();
string Sql = "INSERT INTO fuel (fuelreg, fuelprevmile, fuelcurrentmile, fuellitres, fuelmpg, fueldatetime)"
+ "VALUES("
+ "'" + fuelReg.Text + "', "
+ "'" + fuelFromTextBox.Text + "', "
+ "'" + fuelToTextBox.Text + "', "
+ "'" + fuelLitersTextBox.Text + "', "
+ "'" + fuelMPGTextBox.Text + "', "
+ "'" + fuelDateTimeCombined.Text + "'"
+ "); ";
SqlCommand cmd = new SqlCommand(Sql, con);
SqlDataReader DR = cmd.ExecuteReader();
con.Close();
fuelReg.Text = null;
fuelFromTextBox.Text = null;
fuelToTextBox.Text = null;
fuelLitersTextBox.Text = null;
fuelMPGTextBox.Text = null;
fuelDatePicker.Text = null;
fuelTimePicker.Text = null;
fuelDateTimeCombined.Text = null;
this.fuelTableAdapter.Fill(this.cTDBDataSet.fuel);
}
}
private void FuelDateNowButton_Click(object sender, EventArgs e)
{
fuelDatePicker.Text = DateTime.Now.ToString();
fuelTimePicker.Text = DateTime.Now.ToString();
}
private void FuelPrevMileage_Click(object sender, EventArgs e)
{
}
private void FuelReg_SelectedIndexChanged(object sender, EventArgs e)
{
fuelFromTextBox.ReadOnly = false;
fuelToTextBox.ReadOnly = false;
fuelLitersTextBox.ReadOnly = false;
}
private void FuelBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.fuelBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.cTDBDataSet);
}
private void mileageMinusTextBox_TextChanged(object sender, EventArgs e)
{
decimal parsedValue2;
if (!decimal.TryParse(fuelLitersTextBox.Text, out parsedValue2))
{
}
else
{
fuelGallonsTextBox.Text = (Convert.ToDecimal(fuelLitersTextBox.Text) / Convert.ToDecimal(convTextBox.Text)).ToString();
}
fuelFromTextBox.Refresh();
fuelToTextBox.Refresh();
fuelLitersTextBox.Refresh();
fuelGallonsTextBox.Refresh();
fuelMPGTextBox.Refresh();
mileageMinusTextBox.Refresh();
}
private void FuelLitersTextBox_TextChanged(object sender, EventArgs e)
{
decimal parsedValue;
if (!decimal.TryParse(fuelLitersTextBox.Text, out parsedValue))
{
}
else
{
fuelGallonsTextBox.Text = (Convert.ToDecimal(fuelLitersTextBox.Text) / Convert.ToDecimal(convTextBox.Text)).ToString();
}
fuelFromTextBox.Refresh();
fuelToTextBox.Refresh();
fuelLitersTextBox.Refresh();
fuelGallonsTextBox.Refresh();
fuelMPGTextBox.Refresh();
mileageMinusTextBox.Refresh();
}
private void fuelToTextBox_TextChanged(object sender, EventArgs e)
{
int parsedValue;
if (!int.TryParse(fuelToTextBox.Text, out parsedValue))
{
}
else
{
mileageMinusTextBox.Text = (Convert.ToDecimal(fuelToTextBox.Text) - Convert.ToDecimal(fuelFromTextBox.Text)).ToString();
}
decimal parsedValue2;
if (!decimal.TryParse(fuelLitersTextBox.Text, out parsedValue2))
{
}
else
{
fuelGallonsTextBox.Text = (Convert.ToDecimal(fuelLitersTextBox.Text) / Convert.ToDecimal(convTextBox.Text)).ToString();
}
Decimal parsedValue3;
if (!Decimal.TryParse(fuelGallonsTextBox.Text, out parsedValue3))
{
}
else
{
fuelMPGTextBox.Text = Math.Round((Convert.ToDecimal(mileageMinusTextBox.Text) / Convert.ToDecimal(fuelGallonsTextBox.Text)), 3).ToString();
}
fuelFromTextBox.Refresh();
fuelToTextBox.Refresh();
fuelLitersTextBox.Refresh();
fuelGallonsTextBox.Refresh();
fuelMPGTextBox.Refresh();
mileageMinusTextBox.Refresh();
}
private void fuelGallonsTextBox_TextChanged(object sender, EventArgs e)
{
Decimal parsedValue;
if (!Decimal.TryParse(fuelGallonsTextBox.Text, out parsedValue))
{
}
else
{
fuelMPGTextBox.Text = Math.Round((Convert.ToDecimal(mileageMinusTextBox.Text) / Convert.ToDecimal(fuelGallonsTextBox.Text)),3).ToString();
}
fuelFromTextBox.Refresh();
fuelToTextBox.Refresh();
fuelLitersTextBox.Refresh();
fuelGallonsTextBox.Refresh();
fuelMPGTextBox.Refresh();
mileageMinusTextBox.Refresh();
}
private void fuelRegView_SelectedIndexChanged(object sender, EventArgs e)
{
BindingSource bs = new BindingSource();
bs.DataSource = fuelDataGridView.DataSource;
bs.Filter = "fuelreg like '%" + fuelRegView.Text + "%'";
fuelDataGridView.DataSource = bs;
}
private void tabControl1_Click(object sender, EventArgs e)
{
BindingSource bs = new BindingSource();
bs.DataSource = fuelDataGridView.DataSource;
bs.Filter = "fuelreg like '%'";
fuelDataGridView.DataSource = bs;
fuelRegView.SelectedIndex = -1;
}
private void fuelFromTextBox_TextChanged(object sender, EventArgs e)
{
decimal parsedValue;
if (!decimal.TryParse(fuelToTextBox.Text, out parsedValue))
{
}
else
{
mileageMinusTextBox.Text = (Convert.ToDecimal(fuelToTextBox.Text) - Convert.ToDecimal(fuelFromTextBox.Text)).ToString();
}
decimal parsedValue2;
if (!decimal.TryParse(fuelLitersTextBox.Text, out parsedValue2))
{
}
else
{
fuelGallonsTextBox.Text = (Convert.ToDecimal(fuelLitersTextBox.Text) / Convert.ToDecimal(convTextBox.Text)).ToString();
}
Decimal parsedValue3;
if (!Decimal.TryParse(fuelGallonsTextBox.Text, out parsedValue3))
{
}
else
{
fuelMPGTextBox.Text = Math.Round((Convert.ToDecimal(mileageMinusTextBox.Text) / Convert.ToDecimal(fuelGallonsTextBox.Text)), 3).ToString();
}
fuelFromTextBox.Refresh();
fuelToTextBox.Refresh();
fuelLitersTextBox.Refresh();
fuelGallonsTextBox.Refresh();
fuelMPGTextBox.Refresh();
mileageMinusTextBox.Refresh();
}
}
}
private void fuelFromTextBox_TextChanged(object sender, EventArgs e)
{
double from, to;
if (
!double.TryParse(fuelFromTextBox.Text, out from) ||
!double.TryParse(fuelToTextBox.Text, out to)
)
{
return;
}
mileageMinusTextBox.Text = (to - from).ToString();
decimal mile, gall;
if (
!decimal.TryParse(mileageMinusTextBox.Text, out mile) ||
!decimal.TryParse(fuelGallonsTextBox.Text, out gall)
)
{
return;
}
fuelMPGTextBox.Text = Math.Round((mile / gall), 3).ToString();
decimal litres, conv;
if (
!decimal.TryParse(fuelLitersTextBox.Text, out litres) ||
!decimal.TryParse(convTextBox.Text, out conv)
)
{
return;
}
fuelGallonsTextBox.Text = (litres / conv).ToString();
fuelFromTextBox.Refresh();
fuelToTextBox.Refresh();
fuelLitersTextBox.Refresh();
fuelGallonsTextBox.Refresh();
fuelMPGTextBox.Refresh();
mileageMinusTextBox.Refresh();
fuelFromTextBox.Update();
fuelToTextBox.Update();
fuelLitersTextBox.Update();
fuelGallonsTextBox.Update();
fuelMPGTextBox.Update();
mileageMinusTextBox.Update();
}