下面是代碼:輸入字符串的不正確的格式錯誤
public void calculations()
{
int value;
try
{
if ((string.IsNullOrEmpty(txtrate.Text)) || (string.IsNullOrEmpty(txttotalkm.Text)))
{
MessageBox.Show("Fill both the Values", "Try Again", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if ((int.TryParse(txtrate.Text, out value)) && (int.TryParse(txttotalkm.Text, out value)))
{
int rate = int.Parse(txtrate.Text.Trim());
int km = int.Parse(txttotalkm.Text.Trim());
decimal gross = rate * km;
decimal NBT = (gross * 8/100);
decimal vat = ((gross + NBT) * 11/100);
decimal total = vat + NBT + gross;
string snbt = String.Format("{0:Rs 0.00}", NBT);
string svat = String.Format("{0:Rs 0.00}", vat);
string stotal = String.Format("{0:Rs 0.00}", total);
lblnbt.Visible = true;
lblnbt.Text = snbt;
lblvat.Visible = true;
lblvat.Text = svat;
lbltotal.Visible = true;
lbltotal.Text = stotal;
string ltnbt = lblnbt.Text.ToString();
string ltvat = lblvat.Text.ToString();
string lttotal = lbltotal.Text.ToString();
int inbt = Convert.ToInt32(lblnbt.Text);
int ivat = Convert.ToInt32(lblvat.Text);
int itotal = Convert.ToInt32(lbltotal.Text);
//CreateWordDocument(@"C:\temp\test.docx",
// @"C:\temp\new.docx");
clear();
}
else
{
MessageBox.Show("Wrong Values Please Check again", "Try Again", MessageBoxButtons.OK, MessageBoxIcon.Information);
clear();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
運行程序時,它說:
輸入字符串的不正確的格式錯誤
我必須說我只是想編碼和學習,而不是專業人士,我只是無法找出問題。
試圖消除try...catch
盒,看到了例外,它說的:
如果沒有做到這一點更簡單的方法,只是讓我知道。
你會在哪一行發生異常? –
引起此問題的'int.Parse'使用'int.TryParse'來代替。希望你是第N個人提出這個問題 –
@diiN_ @ int ivat = Convert.ToInt32(lblvat.Text); – Binku