Object does not match target type.

Discussion in 'ASP .Net' started by rn5a@rediffmail.com, Feb 20, 2007.

  1. Guest

    Consider the following code:

    <script runat="server">
    Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
    Dim dInfo As DirectoryInfo

    dInfo = New DirectoryInfo(Server.MapPath("/Folder1"))
    dgFD.DataSource = dInfo.GetFiles("*.*")
    dgFD.DataBind()
    End Sub
    </script>
    <form runat="server">
    <asp:DataGrid ID="dgFD" runat="server"/>
    </form>

    The above code works fine & populates the DataGrid with all the files
    residing in the directory named "Folder1". But it doesn't get the sub-
    directories residing in "Folder1". In order to populate the DataGrid
    with both the files & sub-directories residing in "Folder1", I
    replaced the line

    dgFD.DataSource = dInfo.GetFiles("*.*")

    with

    dgFD.DataSource = dInfo.GetFileSystemInfos("*.*")

    But now when I run the above code, ASP.NET generates the following
    error:

    Object does not match target type.

    pointing to the line

    dgFD.DataBind()

    What is wrong with the above code? How do I populate the DataGrid with
    both the files as well as the sub-directories residing in the
    directory named "Folder1"?
    , Feb 20, 2007
    #1
    1. Advertising

  2. First, have you checked to make sure that you aren't getting a null value or
    empty array when you call the GetFileSystemInfos method? I can't think of
    anything else that may cause it since the FileSystemInfo class is the base
    of the FileInfo.


    --

    Hope this helps,
    Mark Fitzpatrick
    Former Microsoft FrontPage MVP 199?-2006

    <> wrote in message
    news:...
    > Consider the following code:
    >
    > <script runat="server">
    > Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
    > Dim dInfo As DirectoryInfo
    >
    > dInfo = New DirectoryInfo(Server.MapPath("/Folder1"))
    > dgFD.DataSource = dInfo.GetFiles("*.*")
    > dgFD.DataBind()
    > End Sub
    > </script>
    > <form runat="server">
    > <asp:DataGrid ID="dgFD" runat="server"/>
    > </form>
    >
    > The above code works fine & populates the DataGrid with all the files
    > residing in the directory named "Folder1". But it doesn't get the sub-
    > directories residing in "Folder1". In order to populate the DataGrid
    > with both the files & sub-directories residing in "Folder1", I
    > replaced the line
    >
    > dgFD.DataSource = dInfo.GetFiles("*.*")
    >
    > with
    >
    > dgFD.DataSource = dInfo.GetFileSystemInfos("*.*")
    >
    > But now when I run the above code, ASP.NET generates the following
    > error:
    >
    > Object does not match target type.
    >
    > pointing to the line
    >
    > dgFD.DataBind()
    >
    > What is wrong with the above code? How do I populate the DataGrid with
    > both the files as well as the sub-directories residing in the
    > directory named "Folder1"?
    >
    Mark Fitzpatrick, Feb 20, 2007
    #2
    1. Advertising

  3. Hi there guys,

    He gets exception because data source contains two types of objects. Even if
    they inherit from the same base class, data binder checks the actual type (it
    does not matter if you use base class in source array because polymorphic
    call to GetType() will return real type of every element) which can be
    FileInfo or DirectoryInfo. This only happens when you use AutoGenerateColumns
    because, grid control enumerates all properties for every DataItem (meaning
    for every single row). To resolve the problem, declare columns directly (note
    there are some incompatibilities such Name):

    -- begin aspx code --
    <asp:DataGrid runat="server" ID="dgFD" AutoGenerateColumns="false">
    <Columns>
    <asp:BoundColumn DataField="FullName" HeaderText="Name"/>
    <asp:BoundColumn DataField="CreationTime" HeaderText="Created" />
    </Columns>
    </asp:DataGrid>
    -- end aspx code –

    -- begin vb.net code –
    Dim directory As New System.IO.DirectoryInfo(Server.MapPath("~/"))

    If directory.Exists Then

    Dim files() As System.IO.FileSystemInfo = directory.GetFiles()
    Dim directories() As System.IO.FileSystemInfo = directory.GetDirectories()
    Dim both(files.Length + directories.Length) As System.IO.FileSystemInfo

    Array.Copy(files, 0, both, 0, files.Length)
    Array.Copy(directories, 0, both, files.Length - 1, directories.Length)

    dgFD.DataSource = both
    dgFD.DataBind()

    End If
    -- end vb code --

    Hope this helps

    "Mark Fitzpatrick" wrote:

    > First, have you checked to make sure that you aren't getting a null value or
    > empty array when you call the GetFileSystemInfos method? I can't think of
    > anything else that may cause it since the FileSystemInfo class is the base
    > of the FileInfo.
    >
    >
    > --
    >
    > Hope this helps,
    > Mark Fitzpatrick
    > Former Microsoft FrontPage MVP 199?-2006
    >
    > <> wrote in message
    > news:...
    > > Consider the following code:
    > >
    > > <script runat="server">
    > > Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
    > > Dim dInfo As DirectoryInfo
    > >
    > > dInfo = New DirectoryInfo(Server.MapPath("/Folder1"))
    > > dgFD.DataSource = dInfo.GetFiles("*.*")
    > > dgFD.DataBind()
    > > End Sub
    > > </script>
    > > <form runat="server">
    > > <asp:DataGrid ID="dgFD" runat="server"/>
    > > </form>
    > >
    > > The above code works fine & populates the DataGrid with all the files
    > > residing in the directory named "Folder1". But it doesn't get the sub-
    > > directories residing in "Folder1". In order to populate the DataGrid
    > > with both the files & sub-directories residing in "Folder1", I
    > > replaced the line
    > >
    > > dgFD.DataSource = dInfo.GetFiles("*.*")
    > >
    > > with
    > >
    > > dgFD.DataSource = dInfo.GetFileSystemInfos("*.*")
    > >
    > > But now when I run the above code, ASP.NET generates the following
    > > error:
    > >
    > > Object does not match target type.
    > >
    > > pointing to the line
    > >
    > > dgFD.DataBind()
    > >
    > > What is wrong with the above code? How do I populate the DataGrid with
    > > both the files as well as the sub-directories residing in the
    > > directory named "Folder1"?
    > >

    >
    >
    >
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Feb 20, 2007
    #3
  4. Guest

    On Feb 20, 8:23 pm, Milosz Skalecki [MCAD] <>
    wrote:
    > Hi there guys,
    >
    > He gets exception because data source contains two types of objects. Even if
    > they inherit from the same base class, data binder checks the actual type (it
    > does not matter if you use base class in source array because polymorphic
    > call to GetType() will return real type of every element) which can be
    > FileInfo or DirectoryInfo. This only happens when you use AutoGenerateColumns
    > because, grid control enumerates all properties for every DataItem (meaning
    > for every single row). To resolve the problem, declare columns directly (note
    > there are some incompatibilities such Name):
    >
    > -- begin aspx code --
    > <asp:DataGrid runat="server" ID="dgFD" AutoGenerateColumns="false">
    > <Columns>
    > <asp:BoundColumn DataField="FullName" HeaderText="Name"/>
    > <asp:BoundColumn DataField="CreationTime" HeaderText="Created" />
    > </Columns>
    > </asp:DataGrid>
    > -- end aspx code -
    >
    > -- begin vb.net code -
    > Dim directory As New System.IO.DirectoryInfo(Server.MapPath("~/"))
    >
    > If directory.Exists Then
    >
    > Dim files() As System.IO.FileSystemInfo = directory.GetFiles()
    > Dim directories() As System.IO.FileSystemInfo = directory.GetDirectories()
    > Dim both(files.Length + directories.Length) As System.IO.FileSystemInfo
    >
    > Array.Copy(files, 0, both, 0, files.Length)
    > Array.Copy(directories, 0, both, files.Length - 1, directories.Length)
    >
    > dgFD.DataSource = both
    > dgFD.DataBind()
    >
    > End If
    > -- end vb code --
    >
    > Hope this helps
    >
    >
    >
    > "Mark Fitzpatrick" wrote:
    > > First, have you checked to make sure that you aren't getting a null value or
    > > empty array when you call the GetFileSystemInfos method? I can't think of
    > > anything else that may cause it since the FileSystemInfo class is the base
    > > of the FileInfo.

    >
    > > --

    >
    > > Hope this helps,
    > > Mark Fitzpatrick
    > > Former Microsoft FrontPage MVP 199?-2006

    >
    > > <> wrote in message
    > >news:...
    > > > Consider the following code:

    >
    > > > <script runat="server">
    > > > Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
    > > > Dim dInfo As DirectoryInfo

    >
    > > > dInfo = New DirectoryInfo(Server.MapPath("/Folder1"))
    > > > dgFD.DataSource = dInfo.GetFiles("*.*")
    > > > dgFD.DataBind()
    > > > End Sub
    > > > </script>
    > > > <form runat="server">
    > > > <asp:DataGrid ID="dgFD" runat="server"/>
    > > > </form>

    >
    > > > The above code works fine & populates the DataGrid with all the files
    > > > residing in the directory named "Folder1". But it doesn't get the sub-
    > > > directories residing in "Folder1". In order to populate the DataGrid
    > > > with both the files & sub-directories residing in "Folder1", I
    > > > replaced the line

    >
    > > > dgFD.DataSource = dInfo.GetFiles("*.*")

    >
    > > > with

    >
    > > > dgFD.DataSource = dInfo.GetFileSystemInfos("*.*")

    >
    > > > But now when I run the above code, ASP.NET generates the following
    > > > error:

    >
    > > > Object does not match target type.

    >
    > > > pointing to the line

    >
    > > > dgFD.DataBind()

    >
    > > > What is wrong with the above code? How do I populate the DataGrid with
    > > > both the files as well as the sub-directories residing in the
    > > > directory named "Folder1"?- Hide quoted text -

    >
    > - Show quoted text -


    Thanks a lot, Milosz. Your code certainly did help a lot but as you
    have pointed out, there are some incompatibilities like Name. Length
    (used to find the size of a file) is another such incompatibility. How
    do you overcome these incompatibilities?

    Thanks once again....
    , Feb 20, 2007
    #4
  5. Hi there again,

    No problem at all, unfortunatelly we have to do it manually (i little bit
    more coding). BTW i completely forgot vb.net differes from c# in array
    declaration and there was a bug in my last snippet. Please forgive me but i'm
    strictly c# man :) Anyway, it should go as following:

    -- begin aspx page code --

    <asp:DataGrid runat="server" ID="dgFD" AutoGenerateColumns="false">
    <Columns>
    <asp:TemplateColumn>
    <HeaderTemplate>
    Name
    </HeaderTemplate>
    <ItemTemplate>
    <asp:Literal runat="server" ID="name" />
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn>
    <HeaderTemplate>
    Created
    </HeaderTemplate>
    <ItemTemplate>
    <asp:Literal runat="server" ID="created" />
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn>
    <HeaderTemplate>
    Size
    </HeaderTemplate>
    <ItemTemplate>
    <asp:Literal runat="server" ID="Size" />
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:DataGrid>

    -- end aspx page code --

    -- begin vb.net code behind --

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    _
    Handles Me.Load

    If Not IsPostBack Then

    Dim directory As New System.IO.DirectoryInfo(Server.MapPath("~/"))

    If directory.Exists Then

    Dim files() As System.IO.FileSystemInfo = directory.GetFiles()
    Dim directories() As System.IO.FileSystemInfo = directory.GetDirectories()
    Dim both(files.Length + directories.Length - 1) As System.IO.FileSystemInfo

    Array.Copy(files, 0, both, 0, files.Length)
    Array.Copy(directories, 0, both, files.Length, directories.Length)

    dgFD.DataSource = both
    dgFD.DataBind()

    End If

    End If

    End Sub


    Protected Sub dgFD_ItemDataBound(ByVal sender As Object, _
    ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
    Handles dgFD.ItemDataBound

    Dim item As DataGridItem = e.Item

    If item.ItemType = ListItemType.Item Or _
    item.ItemType = ListItemType.AlternatingItem Then

    Dim fileInfo As System.IO.FileSystemInfo = _
    CType(item.DataItem, System.IO.FileSystemInfo)

    Dim literal As Literal

    ' name
    literal = CType(item.FindControl("name"), Literal)
    literal.Text = fileInfo.Name

    ' creation time
    literal = CType(item.FindControl("created"), Literal)
    literal.Text = fileInfo.CreationTime.ToString()

    If TypeOf fileInfo Is System.IO.FileInfo Then
    'size
    literal = CType(item.FindControl("length"), Literal)
    literal.Text = CType(fileInfo, _
    System.IO.FileInfo).Length.ToString()
    End If


    End If

    End Sub


    -- end vb.net code behind --

    Hope it helps

    Milosz


    "" wrote:

    > On Feb 20, 8:23 pm, Milosz Skalecki [MCAD] <>
    > wrote:
    > > Hi there guys,
    > >
    > > He gets exception because data source contains two types of objects. Even if
    > > they inherit from the same base class, data binder checks the actual type (it
    > > does not matter if you use base class in source array because polymorphic
    > > call to GetType() will return real type of every element) which can be
    > > FileInfo or DirectoryInfo. This only happens when you use AutoGenerateColumns
    > > because, grid control enumerates all properties for every DataItem (meaning
    > > for every single row). To resolve the problem, declare columns directly (note
    > > there are some incompatibilities such Name):
    > >
    > > -- begin aspx code --
    > > <asp:DataGrid runat="server" ID="dgFD" AutoGenerateColumns="false">
    > > <Columns>
    > > <asp:BoundColumn DataField="FullName" HeaderText="Name"/>
    > > <asp:BoundColumn DataField="CreationTime" HeaderText="Created" />
    > > </Columns>
    > > </asp:DataGrid>
    > > -- end aspx code -
    > >
    > > -- begin vb.net code -
    > > Dim directory As New System.IO.DirectoryInfo(Server.MapPath("~/"))
    > >
    > > If directory.Exists Then
    > >
    > > Dim files() As System.IO.FileSystemInfo = directory.GetFiles()
    > > Dim directories() As System.IO.FileSystemInfo = directory.GetDirectories()
    > > Dim both(files.Length + directories.Length) As System.IO.FileSystemInfo
    > >
    > > Array.Copy(files, 0, both, 0, files.Length)
    > > Array.Copy(directories, 0, both, files.Length - 1, directories.Length)
    > >
    > > dgFD.DataSource = both
    > > dgFD.DataBind()
    > >
    > > End If
    > > -- end vb code --
    > >
    > > Hope this helps
    > >
    > >
    > >
    > > "Mark Fitzpatrick" wrote:
    > > > First, have you checked to make sure that you aren't getting a null value or
    > > > empty array when you call the GetFileSystemInfos method? I can't think of
    > > > anything else that may cause it since the FileSystemInfo class is the base
    > > > of the FileInfo.

    > >
    > > > --

    > >
    > > > Hope this helps,
    > > > Mark Fitzpatrick
    > > > Former Microsoft FrontPage MVP 199?-2006

    > >
    > > > <> wrote in message
    > > >news:...
    > > > > Consider the following code:

    > >
    > > > > <script runat="server">
    > > > > Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
    > > > > Dim dInfo As DirectoryInfo

    > >
    > > > > dInfo = New DirectoryInfo(Server.MapPath("/Folder1"))
    > > > > dgFD.DataSource = dInfo.GetFiles("*.*")
    > > > > dgFD.DataBind()
    > > > > End Sub
    > > > > </script>
    > > > > <form runat="server">
    > > > > <asp:DataGrid ID="dgFD" runat="server"/>
    > > > > </form>

    > >
    > > > > The above code works fine & populates the DataGrid with all the files
    > > > > residing in the directory named "Folder1". But it doesn't get the sub-
    > > > > directories residing in "Folder1". In order to populate the DataGrid
    > > > > with both the files & sub-directories residing in "Folder1", I
    > > > > replaced the line

    > >
    > > > > dgFD.DataSource = dInfo.GetFiles("*.*")

    > >
    > > > > with

    > >
    > > > > dgFD.DataSource = dInfo.GetFileSystemInfos("*.*")

    > >
    > > > > But now when I run the above code, ASP.NET generates the following
    > > > > error:

    > >
    > > > > Object does not match target type.

    > >
    > > > > pointing to the line

    > >
    > > > > dgFD.DataBind()

    > >
    > > > > What is wrong with the above code? How do I populate the DataGrid with
    > > > > both the files as well as the sub-directories residing in the
    > > > > directory named "Folder1"?- Hide quoted text -

    > >
    > > - Show quoted text -

    >
    > Thanks a lot, Milosz. Your code certainly did help a lot but as you
    > have pointed out, there are some incompatibilities like Name. Length
    > (used to find the size of a file) is another such incompatibility. How
    > do you overcome these incompatibilities?
    >
    > Thanks once again....
    >
    >
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Feb 21, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    12,736
    Sean Foy
    Aug 6, 2006
  2. Lasse Vågsæther Karlsen
    Replies:
    1
    Views:
    1,015
    Lasse Vågsæther Karlsen
    Nov 27, 2007
  3. Karahan Celikel

    Datagrid Binding Error: "Object does not match target type."

    Karahan Celikel, Aug 19, 2003, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    228
    Lewis Wang [MSFT]
    Aug 20, 2003
  4. Karahan Celikel

    Datagrid Binding Error "Object does not match target type"

    Karahan Celikel, Aug 19, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    262
    Lewis Wang [MSFT]
    Aug 20, 2003
  5. Karahan Celikel

    DataGrid Binding Problem : "Object does not match target type."

    Karahan Celikel, Aug 19, 2003, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    212
    Lewis Wang [MSFT]
    Aug 20, 2003
Loading...

Share This Page