Solved

Trying to read a folder of images into a Dataset

Posted on 2011-03-04
14
277 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
  • 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

770 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