0

テキストボックスからデータベースに値を保存しています。テキスト ボックスに入力された数値をデータベースに保存するにはどうすればよいですか。次のことを試しましたが、次のエラーが表示されますInput string was not in a correct format

newCarsRow.CustomerID = Convert.ToInt32(TextBox5.Text);
//I have also tried
newCarsRow.CustomerID = int.Parse(TextBox5.Text);

私はそのようにテキストボックスに入力されたテキストを保存しています

newCarsRow.Surname = TextBox1.Text.ToString();
4

3 に答える 3

2

使用する代わりに

newCarsRow.CustomerID = int.Parse(TextBox5.Text);

使ってみるべきです

int customerID = 0;
if(int.TryParse(TextBox5.Text.Trim(), out customerID))
{
  newCarsRow.CustomerID = customerID;
}
else
{
  // Customer id received from the text box is not a valid int. Do relevant error processing
}

これで、以前に直面していた例外が発生することはなくなり、関連するエラー処理も実行できるようになります。

于 2013-05-13T10:09:27.990 に答える
0

問題は数値形式に関連している可能性がありますか?

NumberStylesを指定して Int32.TryParse(string, NumberStyles, IFormatProvider, out int)を使用し、 IFormatProvider としてNumberFormatInfo.CurrentInfo使用してみてください。

于 2013-05-13T10:20:29.060 に答える