?
Solved

Code Loop to populate datatable which populates gridview

Posted on 2011-10-28
3
Medium Priority
?
358 Views
Last Modified: 2012-05-12
I have log files that need parsing.
One section has folders which are named after the systems they are generated from.
I need to code a loop that opens two log files in each folder and puts data i specify into a datatable that gets output to a gridview.

Below is a short version of my code. Right now it only returns the data from the logs in the first folder. I need it to loop through as many folders as exist in the directory. The number of folders can be between 0 and 6 depending on user settings for the data collection tool.
For Each slotConfigInfo As String In IO.Directory.GetFiles(txtPath.Text & "\StorageSystem\", "*_SlotConfigInfo.log", SearchOption.AllDirectories)
                For Each versionInfo As String In IO.Directory.GetFiles(txtPath.Text & "\StorageSystem\", "*_VersionInfo.log", SearchOption.AllDirectories)

                    Dim slotConfigData As String() = IO.File.ReadAllLines(slotConfigInfo)
                    Dim versionInfoData As String() = IO.File.ReadAllLines(versionInfo)
                    Dim systemName As String = slotConfigData(4).Substring(14)
                    Dim systemModel As String = slotConfigData(8).Substring(15)


                    'Create "FilerData" Data Table
                    Dim Table1 As DataTable
                    Table1 = New DataTable("FilerData")
                    'Declare 1 Row for data
                    Dim Row1 As DataRow

                    Try
                        'Declare column called Filer Name
                        Dim filerName As DataColumn = New DataColumn("Filer Name")
                        'Set Data Type for the column
                        filerName.DataType = System.Type.GetType("System.String")
                        'Add column to table
                        Table1.Columns.Add(filerName)

                        Dim Model As DataColumn = New DataColumn("Model")
                        filerModel.DataType = System.Type.GetType("System.String")
                        Table1.Columns.Add(filerModel)

                        'Declare new row
                        Row1 = Table1.NewRow()
                        'Fill row with data - Item property is used to set the field value
                        Row1.Item(filerName) = systemName
                        Row1.Item(filerModel) = systemModel
                        Table1.Rows.Add(Row1)

                    Catch ex As Exception

                    End Try

                    'Create new Dataset
                    Dim ds As New DataSet()
                    ds = New DataSet()
                    'Add the table to the dataset
                    ds.Tables.Add(Table1)
                    'Bind dataset to a gridview control
                    gridFilerProperties.DataSource = ds
                    gridFilerProperties.DataBind()


                Next
            Next

Open in new window

0
Comment
Question by:newimagent
  • 2
3 Comments
 
LVL 17

Accepted Solution

by:
nepaluz earned 2000 total points
ID: 37048504
        'Create "FilerData" Data Table
        Dim Table1 As DataTable
        Table1 = New DataTable("FilerData")
        'Declare column called Filer Name
        Dim filerName As DataColumn = New DataColumn("filerName")
        'Set Data Type for the column
        filerName.DataType = System.Type.GetType("System.String")
        'Add column to table
        Table1.Columns.Add(filerName)

        Dim filerModel As DataColumn = New DataColumn("filerModel")
        filerModel.DataType = System.Type.GetType("System.String")
        Table1.Columns.Add(filerModel)


        For Each slotConfigInfo As String In IO.Directory.GetFiles(txtPath.Text & "\StorageSystem\", "*_SlotConfigInfo.log", SearchOption.SearchAllSubDirectories)
            For Each versionInfo As String In IO.Directory.GetFiles(txtPath.Text & "\StorageSystem\", "*_VersionInfo.log", SearchOption.SearchAllSubDirectories)

                Dim slotConfigData As String() = IO.File.ReadAllLines(slotConfigInfo)
                Dim versionInfoData As String() = IO.File.ReadAllLines(versionInfo)
                Dim systemName As String = slotConfigData(4).Substring(14)
                Dim systemModel As String = slotConfigData(8).Substring(15)

                'Declare 1 Row for data
                Dim Row1 As DataRow

                Try

                    'Declare new row
                    Row1 = Table1.NewRow()
                    'Fill row with data - Item property is used to set the field value
                    Row1.Item(filerName) = systemName
                    Row1.Item(filerModel) = systemModel
                    Table1.Rows.Add(Row1)

                Catch ex As Exception

                End Try

            Next
        Next

        'Bind grid to DataTable
        gridFilerProperties.DataSource = Table1

Open in new window

0
 
LVL 17

Expert Comment

by:nepaluz
ID: 37048517
Not sure what you are doing with the lines below
Dim systemName As String = slotConfigData(4).Substring(14)
Dim systemModel As String = slotConfigData(8).Substring(15)

Open in new window

however, your loop was creating a new Dataset each run and thus the last one was the only one you saw since the others were "detached" by setting a datasource in each loop. I have also removed the dataset and replaced it with a table.
0
 
LVL 1

Author Closing Comment

by:newimagent
ID: 37048588
EXCELLENT!!!

I need to make a few tweaks to that but I can work it from here.

The reason for those lines you were questioning is that I am cherry picking data out of the log file that is in static locations in the log. Those lines basically say grab line 4 starting at character 14, grab line 8 starting at character 15. there is some data i will have to write expressions to get.

This application development stuff has a steep learning curve for sure. Thank you for the excellent answer!
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

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ā€¦
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Integration Management Part 2
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.
Suggested Courses

621 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