Reply
DataReader NULL Problem
Old 04-09-2007, 06:22 AM DataReader NULL Problem
Average Talker

Posts: 21
I have a website that is supposed to display pictures. The name of the picture files is stored in a database called pictures (duh).

The table goes like this:

id: 1
name: landscape.jpg
width: 300
height: 300
description: A picture of a mountain.

The problem is that sometimes I want width and height to be null, and using the Datalist control that can be a problem, for the Datalist control puts "" whenever there's a null value. Take a look at the code below:

<asp: DataList id="datalist1" border=0 RepeatDirection="Horizontal" RepeatColumns="3" runat="server">
<ItemTemplate>
<table>
<tr>
<td><img src="images/<%# DataBinder.Eval(Container.DataItem,"name") %>" width="<%# DataBinder.Eval(Container.DataItem,"width") %>" /></td>
</tr>
</table>
</ItemTemplate>
</asp: DataList>

The problem is right here:
"<%# DataBinder.Eval(Container.DataItem,"width") %>"

Can I make Datalist ignore the width when it is nul, or at least print out some other valuel? Or do I have to program everything using a DataReader and arrays, as if it was PHP?

__________________________________________________ ______________
http://www.carbotek.org
rpcarnell is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 04-09-2007, 11:58 AM Re: DataReader NULL Problem
ForrestCroce's Avatar
Half Man, Half Amazing

Latest Blog Post:
Talapus Lake in June Snow
Posts: 3,022
Name: Forrest Croce
Location: Seattle, WA
You could change your SQL Query to select ... IsNull(width, 0), IsNull(height, 0) to avoid seeing null values in your .NET code. Otherwise, you would need to use a different coding style and fill your stuff in using code-behind, like C# or VB.

You can iterate a DataReader ( while(dr.Read()) ), and then use if(!dr.IsDbNull("width")) Response.Write("<img ..."); to output code directly, or add it to the text property of a server-side page control.
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Old 04-09-2007, 03:32 PM Re: DataReader NULL Problem
Average Talker

Posts: 21
I thought of a datareader too. That's how I'd do it with PHP, which lacks DataGrids and Repeaters.
rpcarnell is offline
Reply With Quote
View Public Profile
 
Old 04-11-2007, 12:24 AM Re: DataReader NULL Problem
Average Talker

Posts: 21
Here is a solution:

sqlstr = "SELECT ids, name, ISNULL(width,300) as width, height, description FROM pictures ";
SqlDataAdapter MyAdapter = new SqlDataAdapter();
MyAdapter.SelectCommand = new SqlCommand(sqlstr, conPubs);

Look at the first line:
I am telling SQL to replace any null value in width with 300. That's because I intent to give every picture a width value, but not a height value. Browsers will automatically give a value to the height according to the picture's dimensions, so all I need is a width.

The rest looks like jargon but works. Take a look at the line that begins with
<%# ((Convert.IsDBNull ... I am telling ASP.NET to give the picture a height value only if the value in the database is not null. I could've done this in the SQL instruction, but I decided to do it inside the Datalist.

<asp: DataList id="datalist1" border=0 RepeatDirection="Horizontal" RepeatColumns="3" runat="server">
<ItemTemplate>

<img src="http://www.webmaster-talk.com/images/<%# DataBinder.Eval(Container.DataItem,"name")%>"
width = "<%#DataBinder.Eval(Container.DataItem, "width")%>"

<%# ((Convert.IsDBNull(DataBinder.Eval(Container.DataI tem, "height"))) ? "" : "height = " + DataBinder.Eval(Container.DataItem, "height")) %>

alt="<%# DataBinder.Eval(Container.DataItem,"description")% >" />



</ItemTemplate>

</asp: DataList>

It works, but the script looks complicated. I wonder if there's an easier way to do it.

__________________________________________________ ___________

http://www.carbotek.org
rpcarnell is offline
Reply With Quote
View Public Profile
 
Old 04-11-2007, 03:55 AM Re: DataReader NULL Problem
ForrestCroce's Avatar
Half Man, Half Amazing

Latest Blog Post:
Talapus Lake in June Snow
Posts: 3,022
Name: Forrest Croce
Location: Seattle, WA
An easier way to do it - at least slightly - would be to save the SQL code into the database as a view. This is an example of why people use them; to hide the structure of the underlying data from an application.

Although you might be better off filling in the null values with 300 ( or the actual value ) and then disallowing nulls in that column.
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Reply     « Reply to DataReader NULL Problem
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.14314 seconds with 13 queries