Solved

Trying to read a folder of images into a Dataset

Posted on 2011-03-04
14
279 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

726 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