Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Object does not match target type.

Reply
Thread Tools

Object does not match target type.

 
 
rn5a@rediffmail.com
Guest
Posts: n/a
 
      02-20-2007
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">
<aspataGrid 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"?

 
Reply With Quote
 
 
 
 
Mark Fitzpatrick
Guest
Posts: n/a
 
      02-20-2007
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: ups.com...
> 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">
> <aspataGrid 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"?
>



 
Reply With Quote
 
 
 
 
=?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
Guest
Posts: n/a
 
      02-20-2007
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 --
<aspataGrid runat="server" ID="dgFD" AutoGenerateColumns="false">
<Columns>
<asp:BoundColumn DataField="FullName" HeaderText="Name"/>
<asp:BoundColumn DataField="CreationTime" HeaderText="Created" />
</Columns>
</aspataGrid>
-- 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: ups.com...
> > 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">
> > <aspataGrid 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"?
> >

>
>
>

 
Reply With Quote
 
rn5a@rediffmail.com
Guest
Posts: n/a
 
      02-20-2007
On Feb 20, 8:23 pm, Milosz Skalecki [MCAD] <mily...@DONTLIKESPAMwp.pl>
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 --
> <aspataGrid runat="server" ID="dgFD" AutoGenerateColumns="false">
> <Columns>
> <asp:BoundColumn DataField="FullName" HeaderText="Name"/>
> <asp:BoundColumn DataField="CreationTime" HeaderText="Created" />
> </Columns>
> </aspataGrid>
> -- 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

>
> > <r...@rediffmail.com> wrote in message
> >news: oups.com...
> > > 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">
> > > <aspataGrid 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....

 
Reply With Quote
 
=?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
Guest
Posts: n/a
 
      02-21-2007
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 --

<aspataGrid 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>
</aspataGrid>

-- 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] <mily...@DONTLIKESPAMwp.pl>
> 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 --
> > <aspataGrid runat="server" ID="dgFD" AutoGenerateColumns="false">
> > <Columns>
> > <asp:BoundColumn DataField="FullName" HeaderText="Name"/>
> > <asp:BoundColumn DataField="CreationTime" HeaderText="Created" />
> > </Columns>
> > </aspataGrid>
> > -- 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

> >
> > > <r...@rediffmail.com> wrote in message
> > >news: oups.com...
> > > > 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">
> > > > <aspataGrid 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....
>
>

 
Reply With Quote
 
 
 
Reply

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help, get error "Object does not match target type" in one instanceof an app, when others work Lasse Vågsæther Karlsen ASP .Net 1 11-27-2007 02:46 PM
"gridview Object does not match target type" error during binding collection of different type object gui.besse@gmail.com ASP .Net 2 08-06-2006 03:12 PM
Datagrid Binding Error: "Object does not match target type." Karahan Celikel ASP .Net Building Controls 1 08-20-2003 12:57 PM
DataGrid Binding Problem : "Object does not match target type." Karahan Celikel ASP .Net Web Controls 1 08-20-2003 12:56 PM
Datagrid Binding Error "Object does not match target type" Karahan Celikel ASP .Net Datagrid Control 1 08-20-2003 10:35 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57