I have a table that stores the image file path, now in my code i have some conditions:

var UserQuery = 
    (from u in DataContext.UserProfiles where u.UserIDfk == currentUserId select u)
        .FirstOrDefault();

if (UserQuery.Avatar == DBNull.Value.ToString()) 
{ 
   UsersAvatarImage.ImageUrl = "Images/UserAvatar/default_avatar.jpg";
}

if (UserQuery.Avatar != DBNull.Value.ToString()) 
{ 
   UsersAvatarImage.ImageUrl = UserQuery.Avatar; 
}

I'm checking if the value of the column is null and if it is, set the value of the image control to some default image and if it is not show the image stored in database, I run out of idea why it doesn't work.

I've also tried string.IsNullOrEmpty and else clause for the second if or empty double quote instead of DBNull.Value.ToString() but it doesn't worked either.

The weird thing is when I comment the second if condition out the first if meet the condition and show the default image but when I comment the second part back the first part doesn't work

It worth to note that when I inserted the data i used DBNull.Value.ToString() to pass the value as null, how can this column be null and not null at the same time, is there a better way to check this?

有帮助吗?

解决方案

You should just use null instead of DBNull on reference types, or make your value types nullable. Entity Framework will take care of the conversion for you.

其他提示

Well, since you appear to be comparing strings did you try this ? :

Thread

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top