Solved

Trying to read a folder of images into a Dataset

Posted on 2011-03-04
14
275 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
Comment Utility
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
Comment Utility
what would you dim x as?
0
 
LVL 17

Expert Comment

by:nepaluz
Comment Utility
Oh! try
For Each x As String In dirInfo.GetFiles 

Open in new window

0
 

Author Comment

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

Author Comment

by:TheWebGuy38
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:TheWebGuy38
Comment Utility
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
Comment Utility
I need to have some dinner, will get back to you when the kids have their siesta.
0
 

Author Comment

by:TheWebGuy38
Comment Utility
hahahah,, send me some!

ok, thanks for the help!
0
 

Author Comment

by:TheWebGuy38
Comment Utility
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
Comment Utility
Good you got that working.
0
 
LVL 41

Expert Comment

by:graye
Comment Utility
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
Comment Utility
Just the image paths
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now