Question Looping/Array problem (WinForm)

aar0n

Member
Joined
Jan 17, 2008
Messages
19
Location
40 Miles S. of Nashville, TN
Programming Experience
5-10
I know this should be basic looping stuff but I'm hitting a mental snag and could use another set of eyes at this point.

The task is fairly simple. I have built a windows app. which streams database tables into individual text files - one DB table per file. The user uses an XML file to configure the parameters which include TableName and FileName. All works fine until I attempt to extend to allow for multiple tables -> multiple files. I need to get the looping to hit the CreateFile method ONCE PER DATATABLE. Currently I am ending up with the same data table for both files.

In the below example, the user wants to convert 2 tables (company and people) into files...
INPUT:
VB.NET:
<Provider>SQLServer</Provider> 
  <TableName>company,people</TableName>
  <Headers>YES</Headers>
  <Target>C:\Program Files\ODBCExport\Test\</Target>
  <FileName>company.txt,people.txt</FileName>
  <Delimeter>|</Delimeter>



VB.NET:
 Try
            Dim objDAL As New DAL



            Dim files As String() = strFName.Split(New Char() {","})
            Dim tables As String() = strTable.Split(New Char() {","})
            Dim t As Integer = 0
            Dim f As Integer = 0

            
            For Each mytable As String In allables
                table = tables(t)
                Dim dt As DataTable = objDAL.GetDataTable(strProvider, strDBSource, table)  'Gets 1 table
                t += 1

                For Each myFile As String In files
                    Dim strfullpath = strTarget + myFile
                    CreateTextFile(dt, strfullpath, strDelim) 'should create a file based on the above table
                Next


            Next
 Catch oops As Exception
            WriteToErrorLog(oops.Message.ToString)
            lblMessage.Visible = True
            lblMessage.Text = "Database Connection or File Path Error.  See error file for details."
        End Try

I'll provide more info if needed, but I could use a few pointers here. Thanks in advance for any help I might get on here. I'm thinking that the solution is basic looping and I'm just missing it.
-Aaron
 
Last edited:
You're really going against the point of XML with that structure. You really should rethink your XML schema so that each table name and file name is in its own element. Having a single element value containing comma-separated values in it means that you may as well not use XML at all.
 
You're really going against the point of XML with that structure. You really should rethink your XML schema so that each table name and file name is in its own element. Having a single element value containing comma-separated values in it means that you may as well not use XML at all.

...Good point. I was really just looking for something to use for quick and easy configuration determined by the user.

Do you have any suggestions for a format I can use to allow for unlimited number of tables and files?
Thanks.
 
I'd be looking at something like this:
VB.NET:
<Provider>SQLServer</Provider>
<Headers>YES</Headers>
<Target>C:\Program Files\ODBCExport\Test\</Target>
<Delimeter>|</Delimeter>
<tables>
  <table name="company">
    <filename>compant.txt</filename>
  </table>
  <table name="people">
    <filename>people.txt</filename>
  </table>
</tables>
If you want to be able to use a different folder and/or delimiter for each table/file then you'd need to move those elements inside the <table> element.
 
I'd be looking at something like this:
VB.NET:
<Provider>SQLServer</Provider>
<Headers>YES</Headers>
<Target>C:\Program Files\ODBCExport\Test\</Target>
<Delimeter>|</Delimeter>
<tables>
  <table name="company">
    <filename>compant.txt</filename>
  </table>
  <table name="people">
    <filename>people.txt</filename>
  </table>
</tables>
If you want to be able to use a different folder and/or delimiter for each table/file then you'd need to move those elements inside the <table> element.


....makes sense, thanks. I'd imagine that the coding will be easier/cleaner with that structure as well.
 
Note that, ideally, you'd also create an XSD to allow you to validate the XML, e.g. make sure every <table> element has a 'name' attribute and <filename> element.

The XML in this program really isn't being used for the 'true' purpose of XML, but I will take your suggestion and do it the right way, for the sake of getting into good habits. Hopefully my looping issues will be resolved in the process since the approach is more logical. Anyway, thanks again.
 
jmcilhinney, thanks for the advice.

Using a conventional XML structure I was able to easily re-code my application using an XMLReader and it's working great...the code looks a little better too!

I'm looking for the 'resolved' icon. I saw it earlier but it's late and I can't find it now.
 
Last edited:
I'm looking for the 'resolved' icon. I saw it earlier but it's late and I can't find it now.
Edit your first post and change the title prefix.
 
Back
Top