Wrox Programmer Forums
|
BOOK: ASP.NET Website Programming Problem-Design-Solution
This is the forum to discuss the Wrox book ASP.NET Website Programming: Problem - Design - Solution, Visual Basic .NET Edition by Marco Bellinaso, Kevin Hoffman; ISBN: 9780764543869
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET Website Programming Problem-Design-Solution section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 23rd, 2003, 06:18 PM
Authorized User
 
Join Date: Nov 2003
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default For Loop problem with DataSet\DataTable

I have a dataset 'headlines' that contains single DataTable having news headlines and mage information.
Each news may have associated image with it or null if no mage exists for a new.
The dataset has only one DataTable & this DataTable will look like as seen below. Pretty simple.

news_id news_datereleased news_source news_imageurl
----------- --------------------------- ------------------ -----------------------
2 12-29-03 10:20 AP http://../ap_image1.jpg
5 12-29-03 09:30 Reuters NULL
9 12-28-03 11:30 AFP http://../afp_image2.jpg
8 12-27-03 12:23 BBC NULL
5 12-28-03 11:30 CNN http://../cnn_image2.jpg
7 12-27-03 12:23 AP http://../ap_image2.jpg


Now I have a datagrid which is bound to above table. This grid has an image object
and I want to set it's imageUrl property to above 'news_imageurl' column which works fine.

Now the problem: it is showing missing image object for 2nd and 4th row in the grid.
To solve this problem I need to set the visible property of 2nd & 4th row to false.

solution I panned:

headlines.Tables[0].Columns.Add("news_imagevisibility", System.Type.GetType("System.Boolean"), Boolean.FalseString);

so far good. All images are by default NOT Visible on the grid. so my changed DataTable looks like:
news_id news_datereleased news_source news_imageurl news_imagevisibility
----------- --------------------------- ------------------ ----------------------- -----------------------------
2 12-29-03 10:20 AP http://../ap_image1.jpg false
5 12-29-03 09:30 Reuters NULL false
9 12-28-03 11:30 AFP http://../afp_image2.jpg false
8 12-27-03 12:23 BBC NULL false
5 12-28-03 11:30 CNN http://../cnn_image2.jpg false
7 12-27-03 12:23 AP http://../ap_image2.jpg false

Next step is to change 'news_imagevisibility' of the DataTable as shown below
news_id news_datereleased news_source news_imageurl news_imagevisibility
----------- --------------------------- ------------------ ----------------------- -----------------------------
2 12-29-03 10:20 AP http://../ap_image1.jpg true
5 12-29-03 09:30 Reuters NULL false
9 12-28-03 11:30 AFP http://../afp_image2.jpg true
8 12-27-03 12:23 BBC NULL false
5 12-28-03 11:30 CNN http://../cnn_image2.jpg true
7 12-27-03 12:23 AP http://../ap_image2.jpg true

Then in asp page I will have code like this:
 <asp:DataGrid .......>
    <Columns>
       <asp:TemplateColumn>
    <ItemTemplate>
       <asp:image id='..' runat="server" imageurl='<%# DataBinder.Eval(Container.DataItem,"news_imageurl" )%>'
                                  visible='<%# DataBinder.Eval(Container.DataItem,"news_imagevisi bility")%>' >
    <ItemTemplate>
       </asp:TemplateColumn>
       <asp:TemplateColumn> rest of the news info goes here </asp:TemplateColumn>
    </Columns>
 </asp:DataGrid>

Code that I have written:
//use for loop to change all rows in DataTable having valid image
for(int i=0; i<headlines.Tables[0].Rows.Count; i++)
{
  if ((string)headlines.Tables[0].Rows[i]["ni_turl"].GetType() != Type.GetType("System.DBNull"))
     headlines.Tables[0].Rows[i]["news_imagevisibility"]=Boolean.TrueString;
}

The problem is that above loop fails to update the DataTable since it says the cell is READONLY!!!
Do you you how can I get this working???????

Thanks in Advance
Musa

 
Old December 24th, 2003, 05:25 AM
Authorized User
 
Join Date: Nov 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Assuming this data comes from SQL Server, why don't you just fix the query? A simple CASE in the SELECT statement can give you an extra column:
SELECT CASE WHEN news_imageurl is null THEN 0 else 1 END as Visible,
  ...

brian smith


brian
 
Old December 25th, 2003, 03:11 AM
Authorized User
 
Join Date: Nov 2003
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Brian,
 You are the man!
 It worked perfectly and made my code efficient!!
 Everything I am doing withing sql rather than in memory processing.
 Thank you for spending time on my problem.
Musa







Similar Threads
Thread Thread Starter Forum Replies Last Post
Rearrange the column order in a datatable/dataset kiran_q8 ASP.NET 2.0 Professional 2 May 3rd, 2007 08:46 AM
Creating DataSet/DataTable from XML hhmoeller XML 4 March 22nd, 2006 10:41 AM
DataTable belongs to another DataSet bmains ADO.NET 2 December 20th, 2005 12:24 AM
For Loop problem with DataSet\DataTable musa BOOK: Professional C#, 2nd and 3rd Editions 1 December 24th, 2003 09:46 AM
For Loop problem with DataSet\DataTable musa BOOK: Beginning ASP.NET 1.0 0 December 23rd, 2003 06:31 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.