开发者

I am getting Object reference Not set to an instance of an object error in my asp.net mvc aplication

I have this code there Federal_Mandate I am checking weather this MandateType is 1 or 0

if its one I am just converting this as 1 or 0

 mandate.Federal_Mandate = collection["MandateType"].ToString().Equals("Federal") ? Convert.ToByte(1) : Convert.ToByte(0);

and my datbase F开发者_开发问答ederal_mandate datatype has tiinyint.

is that something doing wrong i am here.. why I am gettting object reference error here?

thanks


one of mandate, collection and collection["MandateType"] is null. Set a breakpoint and find out which.


It's pretty hard to figure it out but ... couldn't it be cause your collection["MandateType"] is null?

Maybe you can change it to something like this:

mandate.Federal_Mandate = (collection["MandateType"] ?? "").ToString().Equals("Federal") ? Convert.ToByte(1) : Convert.ToByte(0);


You need to check your collection to see if its null before calling a method on it:

mandate.Federal_Mandate = Convert.ToByte(0);
        if(collection["MandateType"] != null)
        {
            mandate.Federal_Mandate = collection["MandateType"].ToString().Equals("Federal") ? Convert.ToByte(1) : Convert.ToByte(0);
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜