Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Merge Two DataSources Before Binding

Status
Not open for further replies.

ShonGill

Programmer
May 20, 2008
44
US
Hey guys & gals.

I'm having trouble trying to merge data into one dataset from an XML, and also from DirectoryInfo. The problem is I get this error "DataBinding: 'System.IO.FileInfo' does not contain a property with the name 'aid'". 'aid' is one of the nodes in my XML file. I would like to have it and a few other columns in the datagrid from both datasources.

I think the problem is when I try to put the DirectoryInfo() properties into a dataset using the instance.Equals method. I don't get an error during compliation, but the data is still not merging together... or is it. Is there something I neglected to do?


Code:
<%@ Page Language="VB" Debug="true" %>
<script runat="server">

  sub Page_Load(sender as Object, E as EventArgs)
      
      If NOT IsPostBack Then
        LoadFileList

      End if
  End sub


  sub LoadFileList
      
        Dim objdata1 as New DataSet()
        objdata1.ReadXML(Server.Mappath("/APP_DATA/Photos.xml"))
        datagrid2.Datasource = objdata1

        Dim dirInfo as New DirectoryInfo(Server.MapPath("/PHOTOS/"))
        Dim objdata2 as New DataSet()
        objdata2.Equals(dirInfo)
        datagrid2.DataSource = dirInfo.GetFiles("*.jpg")    
        
        Dim keepSchema As Boolean
        keepSchema = True
        objdata1.merge(objdata2, keepSchema)
        datagrid2.DataBind()

  End sub

 Private lastVarValue as Integer
 Function showval(a as integer)
   lastVarValue = a
   Return a
 End Function
Code:
<asp:datagrid id="datagrid2" runat="server" Font-Size="8pt" Font-Name="verdana" Cellpadding="3" AutoGenerateColumns="False" ShowFooter="False" DataKeyField="aid" BorderWidth="1px" BorderColor="#DEBA84" BackColor="#DEBA84" Font-Names="verdana" BorderStyle="None" CellSpacing="2">
                 <Columns>
             <asp:TemplateColumn HeaderText="Id">
                  <ItemTemplate>
                       <%# showval(DataBinder.Eval(Container.DataItem,"aid")) %>
                  </ItemTemplate>
                  <FooterTemplate>
                      <asp:Textbox runat="server" id="aid_add" Text='<%# int32.parse(lastVarValue)+1 %>' Columns="2" />
                  </FooterTemplate>
             </asp:TemplateColumn>
             <asp:HyperLinkColumn DataNavigateUrlField="FullName" DataTextField="Name" 
                      HeaderText="File Name" />
             <asp:BoundColumn DataField="LastWriteTime" HeaderText="Last Write Time"
                      ItemStyle-HorizontalAlign="Center" DataFormatString="{0:d}" />
             <asp:BoundColumn DataField="Length" HeaderText="File Size"
		      ItemStyle-HorizontalAlign="Right" 
		      DataFormatString="{0:#,### bytes}" />
                  </Columns>
             </asp:DataGrid>

Dishon Gillis
Dominion Enterprises Company
 
[tt]objdata2.Equals(dirInfo)[/tt]
this line doesn't do anything because it returns a boolean which you are not assigning a value to and therefore not using anywhere. this line can be deleted.

Code:
datagrid2.Datasource = objdata1
...
datagrid2.DataSource = dirInfo.GetFiles("*.jpg")
objdata1 is over written by an array of strings.

DirectoryInfo and an xml document are totally different objects with nothing in common you can't automatically merge them. to do that you will need to create a mapper object and map one to the other.

I would also recommend moving the vb code to a code behind file rather than interlacing server/client code like this. I would also recommend moving the data access code to a seperate object all together.

the aspx/code behind should only be concerned with the presentation(formatting) of the page. any type of business logic or data access is best kept in separate objects.

here is an example of how I would configure this
Code:
protected override void OnLoad(EventArgs e)
{
   if(!PostBack)
   {
       datagrid2.DataSource = new ImagingService(Server.MapPath("~/")).GetAllImages();
       datagrid2.DataBind();
   }
}
Code:
public class ImagingService
{
   private string root;
   public ImagingService(string root)
   {
       this.root = root;
   }

   public DataTable GetAllImages()
   {
        DataTableimages = DefineImages();
        images.Load(Path.Combine(root, "APP_Data/Photos.xml");

        //assuming there is exactly 1 image per row and the images and rows are in order.
        int index = 0;
        foreach(string file in Directory(Path.Combine(root, "Photos")).GetFiles("*.jpg"))
       {
           images[index++]["Path"] = file;
       }
       return images;
   }

   private DataTableDefineImages()
   {
       DataTable table = new DataTable("images");
       table.Columns.Add("Path", typeof(string));
       table.Columns.Add(...);
       table.Columns.Add(...);
       ...
       return table;
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top