4

これはコードです:

autoInsert.Parameters.Add(new NpgsqlParameter("price", NpgsqlDbType.Numeric));
autoInsert.Parameters[0].Value = txt_price.Text;
        con.Open();
        autoInsert.ExecuteNonQuery();
        con.Close();

クエリを実行すると、「入力文字列が正しい形式ではありませんでした」というエラーが表示されます。その文字列を数値に変換するにはどうすればよいですか。txt_priceはテキストボックスです。

4

2 に答える 2

3
autoInsert.Parameters[0].Value = ConvertToInt32(txt_price.Text); 
于 2012-01-17T14:21:46.877 に答える
2

渡すNumericことで、あなたはあなたが数を渡していることをNpgsqlに保証しました。次に、文字列を渡しました。

他のコードが原因で、に10進値がtxt_priceあり、他に何も存在しない可能性があることをすでに確信している場合は、次を使用します。

autoInsert.Parameters[0].Value = decimal.Parse(txt_price.Text);

それ以外の場合は、他のことを行う前に、コードと組み合わせてこれを確認してください。

decimal price;
if(!decimal.TryParse(txt_price.Text, out price))
{
   //code to display message that txt_price doesn't have a valid value.
   return;
}
using(var con = /*your code that constructs the connection*/)
{
  using(autoInsert = /*your code that returns to command*/)
  {
    autoInsert.Parameters.Add(new NpgsqlParameter("price", NpgsqlDbType.Numeric));
    autoInsert.Parameters[0].Value = price;
    con.Open();
    autoInsert.ExecuteNonQuery();
    con.Close();
  }
}
于 2012-01-17T14:39:09.190 に答える