How can I convert String to Int?

后端 未结 30 2147
情歌与酒
情歌与酒 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:12

    //May be quite some time ago but I just want throw in some line for any one who may still need it
    
    int intValue;
    string strValue = "2021";
    
    try
    {
        intValue = Convert.ToInt32(strValue);
    }
    catch
    {
        //Default Value if conversion fails OR return specified error
        // Example 
        intValue = 2000;
    }
    

提交回复
热议问题