Tuesday, March 27, 2012

Default Image URL in DataList control!

Hey,

I have two questions:

1- In my page there should be a list of all system users, each of them may have an image or may not.

So what I need to do is how to make the next SQL query return a default ImageURL - e.g. "noAvator.jpg" - in case of null image value?

SELECT UserName , image, notes
FROM Members

2- When I try to write an image url in the SQL Server 2005 Management Studio - as "images/noAvator.jpg" or any other name - it always through me this error messagebox:

The changed value is not recignized as valid.
.NET Framework Data Type: Byte[]
Error Message: You can not use the Result pane to set this Field data to value other than NULL.

Why is that? is there a problem with the format of typing the image url?

I appreciate your help!

I'm not sure why you're receiving that error, but how about doing this within your ASPX page instead? If you are displaying your users in some type of GridView, then simply modify the data-binding expression for your image like so:

<asp:imageid="Image1"runat="server"imageurl='<%# (Eval("image") == DBNull.Value) ? "images/noAvator.jpg" : Eval("image") %>'/>

|||

ecbruck:

I'm not sure why you're receiving that error, but how about doing this within your ASPX page instead? If you are displaying your users in some type of GridView, then simply modify the data-binding expression for your image like so:

<asp:imageid="Image1"runat="server"imageurl='<%# (Eval("image") == DBNull.Value) ? "images/noAvator.jpg" : Eval("image") %>'/>

Hiecbruck,

Thanks for you reply,
Just for records, can you give me an example of image type format?

|||

You really shouldn't be using image as a column name, especially not quoted since image is a reserved datatype. Considering that the error message is complaining of trying to convert to a byte array, this leads me to believe that something is getting confused and thinking that you are talking about an actual image. Assuming the image column is of a varchar, or nvarchar datatype, then you can also change your query to be:

SELECT ISNULL(MyColumnNotNamedImage,'images/noAvator.jpg') FROM ...

|||

Motley:

You really shouldn't be using image as a column name, especially not quoted since image is a reserved datatype. Considering that the error message is complaining of trying to convert to a byte array, this leads me to believe that something is getting confused and thinking that you are talking about an actual image. Assuming the image column is of a varchar, or nvarchar datatype, then you can also change your query to be:

SELECT ISNULL(MyColumnNotNamedImage,'images/noAvator.jpg') FROM ...

Thanks alot, Motley!

No comments:

Post a Comment