データベースにあるものに基づいて情報を取得するアプリケーションを作成しました。
ユーザーがデータベースに存在するコードを入力すると、情報が出てきて残りのテキストボックスに入力されます。
ここに私のデータベースがあります:
「 SM0001」と入力したときのプログラムのスクリーンショットを次に示します。
「製品コード テキスト ボックス」、「数量テキスト ボックス」、「説明テキスト ボックス」、「小計テキスト ボックス」、および「合計テキスト ボックス」に「 SM0001 」と入力する前に注意してください。空です。
「製品コード テキスト ボックス」に「SM0001」と入力すると、入力したそのコードに属するすべてのデータが表示されます。
注: データベースの価格はプログラムの小計です
ここに私の問題があります:「製品コードテキストボックス」に「 SM0002 」と入力すると(入力したコードはデータベースにありません。データベースに「 SM0001」しかない製品コード)、プログラムが停止します「入力文字列が正しい形式ではありませんでした」というエラーが表示され、ここで指摘されています:
price = Convert.ToDecimal(this.numericTextBox2.Text);
必要なコードは次のとおりです。
private void textBox_TextChanged(object sender, EventArgs e)
{
UpdatePrice(sender, e);
}
private void UpdatePrice(object sender, EventArgs e)
{
decimal quantity = 0;
decimal price = 0;
int total = 0;
if (numericTextBox1.TextLength == 6)
{
this.numericUpDown1.Enabled = true;
quantity = Convert.ToInt32(this.numericUpDown1.Value);
price = Convert.ToDecimal(this.numericTextBox2.Text);
total = Convert.ToInt32(quantity * price);
if (numericUpDown1.Value > 0)
{
this.numericTextBox3.Text = total.ToString();
}
}
else if (numericTextBox1.TextLength != 6)
{
this.numericUpDown1.Enabled = false;
this.textBox5.Text = "";
this.numericUpDown1.Value = 0;
this.numericTextBox2.Text = "";
this.numericTextBox3.Text = "";
}
else
{
quantity = 0;
price = 0;
total = 0;
MessageBox.Show("There is no data based on your selection", "Error");
}
誰か助けてくれませんか?
「製品コード テキスト ボックスは NumericTextBox1」、「小計テキスト ボックスは NumericTextBox2」、および「合計テキスト ボックスは NumericTextBox3」です。