0

I am trying to get short variables value from my database and need to cast it but it gives an casting error at debugging how can I solve this problem ?Can you help me?

    obj_OBJimagegallery.watermark =String.IsNullOrEmpty((dr["watermark"]).ToString()) ? null : (short?)(dr["watermark"]);      
4

3 回答 3

0

我已经使用这种转换解决了我的问题

     (short?)(Convert.ToInt16(dr["watermark"])
于 2013-05-10T09:29:17.033 回答
0

尝试使用方法,而不是强制转换

Int16.TryParse Method (String, Int16%)

有用法

short result;

if(Int16.TryParse Method ((dr["watermark"].ToString(), out result) == false)
{
//fail
}

obj_OBJimagegallery.watermark = result;// <- you have there parsed value
于 2013-05-10T08:04:46.357 回答
0

尝试这个。

short? s = String.IsNullOrEmpty((dr["watermark"]).ToString()) ? null : (short?)short.Parse(dr["watermark"].ToString());
于 2013-05-10T08:07:20.130 回答