2011-07-15 18 views
1

私は、入力ボックスから取得した文字列を自分のASPフォームで変換して整数に変換しようとしています。ASP.net入力ボックス文字列を整数に変換しますか?

この整数変数は、私が計画したデータベース操作に使用されます。ここで

は私がしようと試みたものである:

string mytxtCHI = txtID.Text; 
      int @CHI = Convert.ToInt16(mytxtCHI); 

エラー:

にSystem.FormatException私が試してみました

物事{ "入力文字列が正しい形式ではありませんでした。"}:

- string mytxtCHI = txtID.Text; 
      int myNewID=(Int32.Parse(txtID.Text)); 

ありがとうございました:)

+2

は、入力ボックスに数字がないようです。テキストは何ですか? – BrokenGlass

+0

入力はどのようになっていますか –

+0

'int.TryParse'を検索します。 –

答えて

1

あなたの方法は正しいですが、あなたが渡している値だかどうか確認するために、ウィンドウまたはSystem.Diagnostics.Debug.WriteLine(mytxtCHI)を見

int myNewID = 0; 

    if(int.TryParse(txtID.Text, out myNewID)) 
    { 
     //your code here 
    } 
    else 
    { 
     //your error handling here 
    } 
+0

デベロッパーありがとう、TryParse Worked – TerryTheTerminator

1

あなたはtryparse使用する必要があります:あなたはInt.TryParseを使用し、値は有効な整数ではありません場合を処理する必要があり

string mytxtCHI = txtID.Text; 
    int number; 
    bool result = Int32.TryParse(mytxtCHI , out number); 
    if (result) 
    { 
    Console.WriteLine("Converted '{0}' to {1}.", mytxtCHI , number);   
    } 
    else 
    { 
    if (value == null) value = ""; 
    Console.WriteLine("Attempted conversion of '{0}' failed.", mytxtCHI); 
    } 
0

string mytxtCHI = txtId.Text; 
int val; 
if (!int.TryParse(mytxtCHI, out val)) 
{ 
    // error here, value was not a valid integer. 
} 
// here, val contains the value, expressed as an integer. 
0

使用をエラーを回避するために何をすべきか、それが空白のですか?

1

Visual Studioの行にブレークポイントを設定します。デバッグモードでブレークポイントまで実行し、txtID.Textにカーソルを合わせると、その内容が表示されます。

ページライフサイクルでこの値をチェックしていますか? Page Initの後にチェックしてください。 HttpRequest.Formをチェックして、何がワイヤーになったのかを確認します。

関連する問題