I am trying to load an image onto a web form depending on the image's URL path inside a database. I can get this to work, however I am trying to display a dummy image incase the record in the database has no image and the column is NULL.
Below is the code that I imagined would work but I receive an error:
Conversion from type 'DBNull' to type 'String' is not valid.
Please can anyone steer me in the right direction or what best practice I should take please? Thanks
'// Load Item Image in imgMenuItem Image Control //
If (Not IsDBNull(Rdr("img_url"))) Then
imgMenuItem.ImageUrl = Rdr("img_url")
imgMenuItem.DataBind()
Else
imgMenuItem.ImageUrl = ("Admin/Images/ItemImageHolder.jpg")
End If
The return type of the Rdr(“img_url”) may be the string. You don’t need to use the IsDBNull() method. You may try the below code.
If( Not String.IsNullOrEmpty(Rdr("img_url"))) Then
imgMenuItem.ImageUrl = Rdr("img_url")
imgMenuItem.DataBind()
Else
imgMenuItem.ImageUrl = ("Admin/Images/ItemImageHolder.jpg")
End If
Thanks for all your responces, however I noticed after I removed the () around the image url (/Admin/Images/ItemImageHolder.jpg) seemed to do the trick just fine. Along with removing the imgMenuItem.DataBind() Unsure why that would have cause a conversion
error but still the following seemed to do the trick:
If (Not IsDBNull(Rdr("img_url"))) Then
imgMenuItem.ImageUrl = Rdr("img_url")
Else
imgMenuItem.ImageUrl = "/Admin/Images/ItemImageHolder.jpg"
End If
Anybody know why this may of happened?
Marked as answer by morrisandy on Feb 19, 2012 04:45 PM
This is a c# conditional if statement, please replace with VB system if you need
imgMenuItem.ImageUrl = (Rdr["img_url"] == DBNull.Value) ? "Admin/Images/ItemImageHolder.jpg" : Rdr["img_url"].ToString();
morrisandy
Member
30 Points
46 Posts
Conversion error when trying to call an image if database value is NULL.
Feb 04, 2012 02:32 AM|LINK
I am trying to load an image onto a web form depending on the image's URL path inside a database. I can get this to work, however I am trying to display a dummy image incase the record in the database has no image and the column is NULL.
Below is the code that I imagined would work but I receive an error:
Conversion from type 'DBNull' to type 'String' is not valid.
Please can anyone steer me in the right direction or what best practice I should take please? Thanks
'// Load Item Image in imgMenuItem Image Control // If (Not IsDBNull(Rdr("img_url"))) Then imgMenuItem.ImageUrl = Rdr("img_url") imgMenuItem.DataBind() Else imgMenuItem.ImageUrl = ("Admin/Images/ItemImageHolder.jpg") End IfGayomard Meh...
Participant
964 Points
241 Posts
Re: Conversion error when trying to call an image if database value is NULL.
Feb 04, 2012 05:14 AM|LINK
Why dont you handle this scenario on the back end ... you could use the SQL Server ISNULL() function to do this...
Qi Wu - MSFT
Contributor
5794 Points
677 Posts
Re: Conversion error when trying to call an image if database value is NULL.
Feb 06, 2012 07:30 AM|LINK
Hi,
The return type of the Rdr(“img_url”) may be the string. You don’t need to use the IsDBNull() method. You may try the below code.
If( Not String.IsNullOrEmpty(Rdr("img_url"))) Then imgMenuItem.ImageUrl = Rdr("img_url") imgMenuItem.DataBind() Else imgMenuItem.ImageUrl = ("Admin/Images/ItemImageHolder.jpg") End IfIf you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework
MetalAsp.Net
All-Star
112231 Points
18268 Posts
Moderator
Re: Conversion error when trying to call an image if database value is NULL.
Feb 06, 2012 07:41 AM|LINK
Try like this: If (Not Convert.IsDBNull(Rdr("img_url")))
Otherwise, put a breakpoint on the line and check what the value of Rdr("img_url") is, to get an idea on how to handle it.
morrisandy
Member
30 Points
46 Posts
Re: Conversion error when trying to call an image if database value is NULL.
Feb 06, 2012 03:06 PM|LINK
Thanks for all your responces, however I noticed after I removed the () around the image url (/Admin/Images/ItemImageHolder.jpg) seemed to do the trick just fine. Along with removing the imgMenuItem.DataBind() Unsure why that would have cause a conversion error but still the following seemed to do the trick:
If (Not IsDBNull(Rdr("img_url"))) Then imgMenuItem.ImageUrl = Rdr("img_url") Else imgMenuItem.ImageUrl = "/Admin/Images/ItemImageHolder.jpg" End IfAnybody know why this may of happened?
tanatrajan
Participant
1784 Points
370 Posts
Re: Conversion error when trying to call an image if database value is NULL.
Feb 06, 2012 04:07 PM|LINK