Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

.NET Forum


You are currently viewing our .NET Forum as a guest. Please register to participate.
Login



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

Posts: 23
Trades: 0
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
 
 
Register now for full access!
Old 04-09-2007, 12:58 PM Re: DataReader NULL Problem
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,023
Name: Forrest Croce
Location: Seattle, WA
Trades: 0
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.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Old 04-09-2007, 04:32 PM Re: DataReader NULL Problem
Average Talker

Posts: 23
Trades: 0
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, 01:24 AM Re: DataReader NULL Problem
Average Talker

Posts: 23
Trades: 0
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, 04:55 AM Re: DataReader NULL Problem
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,023
Name: Forrest Croce
Location: Seattle, WA
Trades: 0
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.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Reply     « Reply to DataReader NULL Problem
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB 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.20963 seconds with 12 queries