Solved

Trying to read a folder of images into a Dataset

Posted on 2011-03-04
14
280 Views
Last Modified: 2013-11-27
I'm trying to read a folder full of images into a datast, and I think I'm close, but not quiet there

the code has an error at  For Each dirInfo In Dir.GetDirectories()

saying 'getdirectories' is not a member of 'string'

not sure if this line is right, or what the glitch is

any help would be greatly appreciated



 
Dim Ds As DataSet
        Dim myColumn As DataColumn
        ' Create new DataColumn, set DataType, ColumnName and add to DataTable.    
        myColumn = New DataColumn()
        myColumn.DataType = System.Type.GetType("System.String")
        myColumn.ColumnName = "ImageUrlField"
        Ds.Tables("Banners").Columns.Add(myColumn)

        Dim dirInfo As New DirectoryInfo(Server.MapPath("~/Images/HeaderImages/"))

        For Each dirInfo In Dir.GetDirectories()

            DataRow(dr = Ds.Tables("Banners").Rows.NewRow())
            dr("ImageUrlField") = dirInfo.GetFiles
            Ds.Tables("Banners").Rows.Add(dr)

        Next

        DataList1.DataSource = Ds
        DataList1.DataBind()

Open in new window

0
Comment
Question by:TheWebGuy38
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
14 Comments
 
LVL 17

Expert Comment

by:nepaluz
ID: 35035116
try

For Each x In dirInfo.GetFiles

Open in new window


and amend your following code to reflect this change
0
 

Author Comment

by:TheWebGuy38
ID: 35035124
what would you dim x as?
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 35035146
Oh! try
For Each x As String In dirInfo.GetFiles 

Open in new window

0
Certified OpenStack Administrator Course

We just refreshed our COA course based on the Newton exam.  With 14 labs, this course goes over the different OpenStack services that are part of the certification: Dashboard, Identity Service, Image Service, Networking, Compute, Object Storage, Block Storage, and Orchestration.

 

Author Comment

by:TheWebGuy38
ID: 35035149
now I get system.io cannot be converted to string :(
0
 

Author Comment

by:TheWebGuy38
ID: 35035198
shouldn't it be something like this?

     Dim dirInfo As New DirectoryInfo(Server.MapPath("~/Images/HeaderImages/"))

        Dim Files As New FileInfo

        For Each Files In dirInfo.GetFiles
0
 
LVL 17

Accepted Solution

by:
nepaluz earned 500 total points
ID: 35035199
I added a definition for the datarow (was not in your code) and changed your code a bit too!

                Dim Ds As DataSet
                Dim myColumn As DataColumn
                myColumn = New DataColumn()
                myColumn.DataType = System.Type.GetType("System.String")
                myColumn.ColumnName = "ImageUrlField"
                Ds.Tables("Banners").Columns.Add(myColumn)
                Dim dr As DataRow
                Dim xDir = Directory.GetFiles(Server.MapPath("~/Images/HeaderImages/"))
                For Each x As String In xDir
                    dr = Ds.Tables("Banners").NewRow()
                    dr("ImageUrlField") = x
                    Ds.Tables("Banners").Rows.Add(dr)
                Next

Open in new window

0
 

Author Comment

by:TheWebGuy38
ID: 35035316
interesting, get this

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 16:         myColumn.DataType = System.Type.GetType("System.String")
Line 17:         myColumn.ColumnName = "ImageUrlField"
Line 18:         Ds.Tables("Banners").Columns.Add(myColumn)
Line 19:         Dim dr As DataRow
Line 20:         Dim xDir = Directory.GetFiles(Server.MapPath("~/Images/HeaderImages/"))
0
 

Author Comment

by:TheWebGuy38
ID: 35035361
i tried this but was having an error at this " Ds.Tables("Banners").Columns.Add(myColumn)"
as well


 
Dim Ds As DataSet
        Dim myColumn As DataColumn
        ' Create new DataColumn, set DataType, ColumnName and add to DataTable.    
        myColumn = New DataColumn()
        myColumn.DataType = System.Type.GetType("System.String")
        myColumn.ColumnName = "ImageUrlField"
        Ds.Tables("Banners").Columns.Add(myColumn)

        Dim dirInfo As New DirectoryInfo(Server.MapPath("~/Images/HeaderImages/"))

        Dim File As FileInfo

        For Each File In dirInfo.GetFiles


            For Each dr As DataRow In Ds.Tables("Banners").Rows
                dr("ImageUrlField") = File.FullName
                Ds.Tables("Banners").Rows.Add(dr)


            Next
        Next
        DataList1.DataSource = Ds
        DataList1.DataBind()

Open in new window

0
 
LVL 17

Expert Comment

by:nepaluz
ID: 35035426
I need to have some dinner, will get back to you when the kids have their siesta.
0
 

Author Comment

by:TheWebGuy38
ID: 35035475
hahahah,, send me some!

ok, thanks for the help!
0
 

Author Comment

by:TheWebGuy38
ID: 35035822
WOOHOOO!!!! got it working

I am posting two blocks of code. one that works, and one that does not.
just two different ways of doing it


Not working:

        Dim Ds As New DataSet
        Dim myColumn1 As DataColumn
       
        Dim Tables As DataTable = New DataTable
        Tables.TableName = "Banners"
        Ds.Tables.Add("Banners")

        'Create new DataColumn, set DataType, ColumnName and add to DataTable.    
        myColumn1 = New DataColumn()
        myColumn1.DataType = System.Type.GetType("System.String")
        myColumn1.ColumnName = "ImageUrlField"
        Ds.Tables("Banners").Columns.Add(myColumn1)

        Dim dirInfo As New DirectoryInfo(Server.MapPath("~/Images/HeaderImages/"))
        Dim File As FileInfo

        For Each File In dirInfo.GetFiles
            For Each dr As DataRow In Ds.Tables("Banners").Rows
                dr("ImageUrlField") = File.FullName
                Ds.Tables("Banners").Rows.Add(dr)
            Next
        Next
        DataList1.DataSource = Ds
        DataList1.DataBind()



Working:
        Dim Ds As New DataSet
        Dim myColumn1 As DataColumn

        Dim Tables As DataTable = New DataTable
        Tables.TableName = "Banners"
        Ds.Tables.Add("Banners")

        ' Create new DataColumn, set DataType, ColumnName and add to DataTable.    
        myColumn1 = New DataColumn()
        myColumn1.DataType = System.Type.GetType("System.String")
        myColumn1.ColumnName = "ImageUrlField"
        Ds.Tables("Banners").Columns.Add(myColumn1)

        Dim dr As DataRow
        Dim xDir = Directory.GetFiles(Server.MapPath("~/Images/HeaderImages/"))
        For Each x As String In xDir
            dr = Ds.Tables("Banners").NewRow()
            dr("ImageUrlField") = x
            Ds.Tables("Banners").Rows.Add(dr)
        Next

        DataList1.DataSource = Ds
        DataList1.DataBind()

Open in new window

0
 
LVL 17

Expert Comment

by:nepaluz
ID: 35036813
Good you got that working.
0
 
LVL 41

Expert Comment

by:graye
ID: 35132316
Just a question.... were you trying to encode just the path to the images (such as "c:\somedirectory\someimage.jpg")... or were you trying to actually encode the images themselves?
0
 

Author Comment

by:TheWebGuy38
ID: 35291717
Just the image paths
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question