C# parse string “0” to integer

前端 未结 8 949
抹茶落季
抹茶落季 2021-01-18 02:58

I have a new laptop at work and code that worked earlier in the week does not work today.

The code that worked before is, simplified:

while (dr.Read         


        
8条回答
  •  孤城傲影
    2021-01-18 03:32

    I think it's generally not considered a good idea to call Convert.ToInt32 for the value reading out of database, what about the value is null, what about the value cannot be parsed. Do you have any exception handling code here.

    1. Make sure the value is not null.
    2. Check the value can be parsed before call Int32.Parse. Consider Int32.TryParse.
    3. consider use a nullable type like int? in this case.

    HTH.

提交回复
热议问题