How can I convert String to Int?

后端 未结 30 2224
情歌与酒
情歌与酒 2020-11-21 05:35

I have a TextBoxD1.Text and I want to convert it to an int to store it in a database.

How can I do this?

30条回答
  •  一整个雨季
    2020-11-21 06:28

    As explained in the TryParse documentation, TryParse() returns a Boolean which indicates that a valid number was found:

    bool success = Int32.TryParse(TextBoxD1.Text, out val);
    
    if (success)
    {
        // Put val in database
    }
    else
    {
        // Handle the case that the string doesn't contain a valid number
    }
    

提交回复
热议问题