473,320 Members | 2,109 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Object does not match target type.

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 '07 #1
4 6234
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

<rn**@rediffmail.comwrote in message
news:11**********************@h3g2000cwc.googlegro 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">
<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 '07 #2
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

<rn**@rediffmail.comwrote in message
news:11**********************@h3g2000cwc.googlegro 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">
<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 '07 #3
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 --
<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
<r...@rediffmail.comwrote in message
news:11**********************@h3g2000cwc.googlegro 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">
<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 '07 #4
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
"rn**@rediffmail.com" 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 --
<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
<r...@rediffmail.comwrote in message
>news:11**********************@h3g2000cwc.googlegr 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">
<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 21 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Dent2 | last post by:
I wrote a nice C# excel routine to automate the formatting of some raw CSV data. I wrote the routine on a WinXP computer with Visual Studio .NET 2003 and Office XP installed. It compiled and ran...
7
by: Clint Herron | last post by:
Howdy! I posted this question on CSharpCorner.com, but then realized I should probably post it on a more active newsgroup. This will be my only cross-post. I'm creating a game engine, and...
0
by: gui.besse | last post by:
It seems that we can't bind a collection of instance of different type. Let's have an example: // My POCO public interface ITest { string Name { get;set;} } public class A : ITest {
3
by: Byron | last post by:
I'm trying to iterate stored parameters and populate any public properties of an object with the parameter value. I've suceeded in doing it the other way around, but when I try it to the object I...
1
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that...
12
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, I have a class which "BiologySequence" which looks about like this. public class BiologySequence { private string _Sequence; public string Sequence {
2
by: =?Utf-8?B?U3dhcHB5?= | last post by:
hi, I am working on application in this i am using two files. In first (consider A) file i am calling the function of other file (consider B). In that function of file B i am calling the method...
4
by: =?Utf-8?B?U3dhcHB5?= | last post by:
hi, Im running this code public Boolean IsLayoutOpen(String strLayoutName) { Layout Layout_Obj = null; try { Layouts layouts = instrumentation.Layouts; //This call is through COM object
2
by: tridirk | last post by:
Hi; I am getting a Objceted Expected Error on my forum site. I can't find what is wrong? Line: Char: Error: Object expected Code:0 the site is My SMF Forum
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.