Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Trying to read a folder of images into a Dataset

Posted on 2011-03-04
14
Medium Priority
?
283 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 2000 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 feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

963 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