Problem to codes

Hi,
there is no output file generated due to the writer below

        For Each companyCode In ds.Select(Function(x) x.CompanyCode).Distinct
            Using writer As StreamWriter = New StreamWriter("c:\\out1.txt")
                writer.WriteLine("33 " + companyCode)
            End Using
            For Each journalCode In ds.Where(Function(x) x.CompanyCode.Equals(companyCode, StringComparison.OrdinalIgnoreCase)).Select(Function(x) x.JournalCode).Distinct
                GenerateFile(companyCode, journalCode, dataRangeCollection)
            Next
        Next

Open in new window

what can I further check, for the reason of this?
LVL 12
HuaMin ChenProblem resolverAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
Permissions problem?  On numbers of windows systems writing to the root directory is not so easy.
Try creating a directory (eg.  C:\test) and writing to a file in that.

For Each companyCode In ds.Select(Function(x) x.CompanyCode).Distinct
            Using writer As StreamWriter = New StreamWriter("c:\\test\\out1.txt")  <<---------
                writer.WriteLine("33 " + companyCode)
            End Using
            For Each journalCode In ds.Where(Function(x) x.CompanyCode.Equals(companyCode, StringComparison.OrdinalIgnoreCase)).Select(Function(x) x.JournalCode).Distinct
                GenerateFile(companyCode, journalCode, dataRangeCollection)
            Next
        Next

Open in new window


ps.  Your code only writes the 33 plus the company code - do you want the using outside the loop?

            Using writer As StreamWriter = New StreamWriter("c:\\out1.txt")
For Each companyCode In ds.Select(Function(x) x.CompanyCode).Distinct
                writer.WriteLine("33 " + companyCode)
            For Each journalCode In ds.Where(Function(x) x.CompanyCode.Equals(companyCode, StringComparison.OrdinalIgnoreCase)).Select(Function(x) x.JournalCode).Distinct
                GenerateFile(companyCode, journalCode, dataRangeCollection)
            Next
        Next
            End Using

Open in new window

HuaMin ChenProblem resolverAuthor Commented:
Right before the above codes, I really am able to write to "c:\".

The problem is, how to capture the reason why the above writer part codes are not able to create file, as expected.
HuaMin ChenProblem resolverAuthor Commented:
Any other way to capture any other reasons there?
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

AndyAinscowFreelance programmer / ConsultantCommented:
>>Right before the above codes, I really am able to write to "c:\".


How?  Is it the same way with a stream writer?  (Please show some code you use)
HuaMin ChenProblem resolverAuthor Commented:
Yes, same way.
AndyAinscowFreelance programmer / ConsultantCommented:
Is the loop entered?  Put a breakpoint and check, maybe there si nothing for the foreach loop.
HuaMin ChenProblem resolverAuthor Commented:
If the part cannot be executed, how to find out more reasons, to the following codes?

        ds = New ExportInterComSalesDataToSunAccountDataSource(dataRangeCollection, ReportOption.GetDatabases, conn)
        TotalLoadedRows += ds.LoadReportData
        CloseConnection(conn)

        For Each companyCode In ds.Select(Function(x) x.CompanyCode).Distinct
            For Each journalCode In ds.Where(Function(x) x.CompanyCode.Equals(companyCode, StringComparison.OrdinalIgnoreCase)).Select(Function(x) x.JournalCode).Distinct
                GenerateFile(companyCode, journalCode, dataRangeCollection)
            Next
        Next

Open in new window

HuaMin ChenProblem resolverAuthor Commented:
And here is

Public Class ExportInterComSalesDataToSunAccountDataSource
    Inherits ExportDataToSunAccountDataSourceBase

    Public Sub New(ByVal reportOptions As ISalesReportOptions,
          Optional ByVal Databases() As String = Nothing,
          Optional ByVal dataConnection As MySqlConnection = Nothing)
        MyBase.New(reportOptions, Databases, dataConnection)
        DocumentCode = "INTERCOM SALES"
    End Sub

    Public Sub New(ByVal dataRanges As DataRangeOptionCollection,
          Optional ByVal Databases() As String = Nothing,
          Optional ByVal dataConnection As MySqlConnection = Nothing)
        MyBase.New(dataRanges, Databases, dataConnection)
        DocumentCode = "INTERCOM SALES"
    End Sub

    Private brandList As BrandCollection

    Public Overrides Function LoadReportData() As Integer
        Dim loadCount As Integer = 0
        Dim conn As MySqlConnection = OpenConnection()
        If brandList Is Nothing Then
            brandList = New BrandCollection
            brandList.LoadFromDb(conn)
        End If
        loadCount += LoadSalesData(conn)
        loadCount += LoadPaymentData(conn)
        CloseConnection()
        Return loadCount
    End Function

    Public Function LoadSalesData(conn As MySqlConnection) As Integer
        Dim rtv As Integer = 0
        Dim sqls() As String = Nothing
        If OptionForm IsNot Nothing Then
            sqls = GetQueries(Function(db) GetSalesDataSqlProductLevel(db, OptionForm))
        Else
            sqls = GetQueries(Function(db) GetSalesDataSqlProductLevel(db, DataRanges, True))
        End If
        Dim sql As String = String.Join(" union all ", sqls)
        Dim data As DataTable = GetDataTable(sql, conn)
        Dim companyCode As String = ""
        Dim journalCode As String = ""
        Dim accountCode As String = ""
        Dim description As String = ""
        Dim transactionRef As String = ""
        Dim tdate As Date = Nothing
        Dim debitCredit As String = ""
        Dim brandName As String = ""
        Dim brandCode As String = ""
        Dim productTypeCode As String = ""
        Dim department As String = ""
        Dim staffNo As String = ""
        Dim shopCode As String = ""
        Dim shopName As String = ""
        Dim stakeholder As String = ""
        Dim analysisValue As String = ""
        Dim accCodeItem As SunAccountCodeItem
        For Each row As DataRow In data.Rows
            journalCode = ""
            accountCode = ""
            debitCredit = ""
            description = "Sales Import"
            transactionRef = ""
            tdate = row(TXNDATE_FIELD.AliasName).ToString.Trim
            productTypeCode = row(PRODUCTTYPECODE_FIELD.AliasName).ToString.Trim
            department = "SHOES"
            brandName = row(PRODUCTBRANDNAME_FIELD.AliasName).ToString.Trim
            If brandList.Contains(brandName) Then brandCode = brandList.Item(brandName).BrandCodes.Min()
            shopCode = row(SHOPCODE_FIELD.AliasName).ToString.Trim
            shopName = FromDbText(row(SHOPNAME_FIELD.AliasName).ToString.Trim)
            companyCode = GetSunAccountCompanyCode(row(DBGROUP_FIELD.AliasName).ToString)
            If GetSunShopCompany(shopCode) <> "" Then companyCode = GetSunShopCompany(shopCode)
            staffNo = ""
            stakeholder = ""
            analysisValue = String.Format("{0}.{1}.{2}.{3}", companyCode, shopCode, FOR_ALL, C_SALES)
            accCodeItem = GetSunAccountCode(DocumentCode, analysisValue)
            If accCodeItem IsNot Nothing Then
                journalCode = accCodeItem.JournalCode
                accountCode = accCodeItem.AccountCode
                debitCredit = accCodeItem.DebitCredit
                transactionRef = String.Format("{0}{1:yyyyMMdd}{2}", journalCode, tdate, shopCode)
                Dim theItem As ExportDataToSunAccountItem = item(companyCode, journalCode, accountCode, description, transactionRef, tdate, debitCredit, productTypeCode, department, brandCode, staffNo, shopCode, stakeholder)
                If theItem Is Nothing Then
                    theItem = New ExportDataToSunAccountItem(companyCode, journalCode, accountCode, description, transactionRef, tdate, debitCredit, productTypeCode, department, brandCode, staffNo, shopCode, stakeholder)
                    Add(theItem)
                End If
                theItem.Amount += row("damount")
            End If
            rtv += 1
        Next
        Return rtv
    End Function

    Public Function LoadPaymentData(conn As MySqlConnection) As Integer
        Dim rtv As Integer = 0
        Dim ds As FinanceDailyPaymentDataSource
        If OptionForm IsNot Nothing Then
            ds = New FinanceDailyPaymentDataSource(OptionForm, Databases, conn)
        Else
            ds = New FinanceDailyPaymentDataSource(DataRanges, Databases, conn)
        End If
        rtv += ds.LoadReportData()
        Dim companyCode As String = ""
        Dim journalCode As String = ""
        Dim accountCode As String = ""
        Dim description As String = ""
        Dim transactionRef As String = ""
        Dim tdate As Date = Nothing
        Dim debitCredit As String = ""
        Dim brandName As String = ""
        Dim brandCode As String = ""
        Dim productTypeCode As String = ""
        Dim department As String = ""
        Dim staffNo As String = ""
        Dim shopCode As String = ""
        Dim shopName As String = ""
        Dim stakeholder As String = ""
        Dim totalAmount As Double = 0.0
        Dim bankCharges As Double = 0.0
        Dim analysisValue As String = ""
        Dim accCodeItem As SunAccountCodeItem
        For Each country In ds
            For Each shop In country
                For Each docDate In shop
                    For Each paymentItem In docDate
                        accountCode = ""
                        journalCode = ""
                        debitCredit = ""
                        description = "Sales Import"
                        transactionRef = ""
                        tdate = docDate.DocumentDate
                        productTypeCode = ""
                        department = "SHOES"
                        brandName = ""
                        shopCode = shop.ShopAlias
                        shopName = shop.ShopName
                        companyCode = GetSunAccountCompanyCode(shop.DB)
                        If GetSunShopCompany(shopCode) <> "" Then companyCode = GetSunShopCompany(shopCode)
                        staffNo = ""
                        stakeholder = ""
                        bankCharges = Math.Round(paymentItem.Value * paymentItem.PaymentChgRate, 2)
                        totalAmount = paymentItem.Value
                        Dim theItem As ExportDataToSunAccountItem
                        analysisValue = String.Format("{0}.{1}.{2}.{3}", companyCode, shopCode, paymentItem.Code, C_BANKAMT)
                        accCodeItem = GetSunAccountCode(DocumentCode, analysisValue)
                        If accCodeItem Is Nothing Then
                            analysisValue = String.Format("{0}.{1}.{2}.{3}", companyCode, shopCode, FOR_ALL, C_BANKAMT)
                            accCodeItem = GetSunAccountCode(DocumentCode, analysisValue)
                        End If
                        If accCodeItem IsNot Nothing Then
                            journalCode = accCodeItem.JournalCode
                            accountCode = accCodeItem.AccountCode
                            debitCredit = accCodeItem.DebitCredit
                            transactionRef = String.Format("{0}{1:yyyyMMdd}{2}", journalCode, tdate, shopCode)
                            theItem = item(companyCode, journalCode, accountCode, description, transactionRef, tdate,
                                           debitCredit, productTypeCode, department, brandCode, staffNo, shopCode, stakeholder)
                            If theItem Is Nothing Then
                                theItem = New ExportDataToSunAccountItem(companyCode, journalCode, accountCode, description, transactionRef,
                                                                         tdate, debitCredit, productTypeCode, department, brandCode, staffNo, shopCode, stakeholder)
                                Add(theItem)
                            End If
                            theItem.Amount += (totalAmount - bankCharges)
                        End If
                        analysisValue = String.Format("{0}.{1}.{2}.{3}", companyCode, shopCode, paymentItem.Code, C_BANKCHARGES)
                        accCodeItem = GetSunAccountCode(DocumentCode, analysisValue)
                        If accCodeItem Is Nothing Then
                            analysisValue = String.Format("{0}.{1}.{2}.{3}", companyCode, shopCode, FOR_ALL, C_BANKCHARGES)
                            accCodeItem = GetSunAccountCode(DocumentCode, analysisValue)
                        End If
                        If accCodeItem IsNot Nothing Then
                            journalCode = accCodeItem.JournalCode
                            accountCode = accCodeItem.AccountCode
                            debitCredit = accCodeItem.DebitCredit
                            transactionRef = String.Format("{0}{1:yyyyMMdd}{2}", journalCode, tdate, shopCode)
                            theItem = item(companyCode, journalCode, accountCode, description,
                                           transactionRef, tdate, debitCredit, productTypeCode,
                                           department, brandCode, staffNo, shopCode, stakeholder)
                            If theItem Is Nothing Then
                                theItem = New ExportDataToSunAccountItem(companyCode, journalCode, accountCode, description,
                                                                         transactionRef, tdate, debitCredit, productTypeCode,
                                                                         department, brandCode, staffNo, shopCode, stakeholder)
                                Add(theItem)
                            End If
                            theItem.Amount += (bankCharges)
                        End If
                        analysisValue = String.Format("{0}.{1}.{2}.{3}", companyCode, shopCode, paymentItem.Code, C_ACCRECEIVABLE)
                        accCodeItem = GetSunAccountCode(DocumentCode, analysisValue)
                        If accCodeItem Is Nothing Then
                            analysisValue = String.Format("{0}.{1}.{2}.{3}", companyCode, shopCode, FOR_ALL, C_ACCRECEIVABLE)
                            accCodeItem = GetSunAccountCode(DocumentCode, analysisValue)
                        End If
                        If accCodeItem IsNot Nothing Then
                            journalCode = accCodeItem.JournalCode
                            accountCode = accCodeItem.AccountCode
                            debitCredit = accCodeItem.DebitCredit
                            transactionRef = String.Format("{0}{1:yyyyMMdd}{2}", journalCode, tdate, shopCode)
                            theItem = item(companyCode, journalCode, accountCode, description, transactionRef, tdate, debitCredit,
                                           productTypeCode, department, brandCode, staffNo, shopCode, stakeholder)
                            If theItem Is Nothing Then
                                theItem = New ExportDataToSunAccountItem(companyCode, journalCode, accountCode, description, transactionRef,
                                                                         tdate, debitCredit, productTypeCode, department, brandCode,
                                                                         staffNo, shopCode, stakeholder)
                                Add(theItem)
                            End If
                            theItem.Amount += (totalAmount)
                        End If
                    Next
                Next
            Next
        Next
        Return rtv
    End Function
End Class

Open in new window

HuaMin ChenProblem resolverAuthor Commented:
I want to capture some values in detail, that is being used by "For Each ..." part, how?
HuaMin ChenProblem resolverAuthor Commented:
Any other help?
AndyAinscowFreelance programmer / ConsultantCommented:
I think your original question is partially solved - you know why the file isn't being created/written to.
HuaMin ChenProblem resolverAuthor Commented:
No, I still cannot step further, as I did originally want to step to the next.
HuaMin ChenProblem resolverAuthor Commented:
By this thread.
it_saigeDeveloperCommented:
If the file is not being generated and you are not getting an exception then the obvious answer is that you did not get any data back from your query.

You already have mechanisms to check to ensure you have rows; e.g. -
ds = New ExportInterComSalesDataToSunAccountDataSource(dataRangeCollection, ReportOption.GetDatabases, conn)
TotalLoadedRows += ds.LoadReportData
CloseConnection(conn)

If TotalLoadedRows > 0 Then
	For Each companyCode In ds.Select(Function(x) x.CompanyCode).Distinct
		System.Diagnostics.Debug.WriteLine(String.Format("Working with company - {0}", companyCode);
		For Each journalCode In ds.Where(Function(x) x.CompanyCode.Equals(companyCode, StringComparison.OrdinalIgnoreCase)).Select(Function(x) x.JournalCode).Distinct
			System.Diagnostics.Debug.WriteLine(String.Format("Working with journal code - {0}", journalCode);
			GenerateFile(companyCode, journalCode, dataRangeCollection)
		Next
	Next
Else
	System.Diagnostics.Debug.WriteLine("No rows were returned...");
End If

Open in new window

If you are getting an exception but want to know more about it, then you probably want to use something like the following -
Try
	ds = New ExportInterComSalesDataToSunAccountDataSource(dataRangeCollection, ReportOption.GetDatabases, conn)
	TotalLoadedRows += ds.LoadReportData
	CloseConnection(conn)

	If TotalLoadedRows > 0 Then
		For Each companyCode In ds.Select(Function(x) x.CompanyCode).Distinct
			System.Diagnostics.Debug.WriteLine(String.Format("Working with company - {0}", companyCode);
			For Each journalCode In ds.Where(Function(x) x.CompanyCode.Equals(companyCode, StringComparison.OrdinalIgnoreCase)).Select(Function(x) x.JournalCode).Distinct
				System.Diagnostics.Debug.WriteLine(String.Format("Working with journal code - {0}", journalCode);
				GenerateFile(companyCode, journalCode, dataRangeCollection)
			Next
		Next
	Else
		System.Diagnostics.Debug.WriteLine("No rows were returned...");
	End If
Catch ex As Exception
	System.Diagnostics.Debug.WriteLine(String.Format("An exception was reported; {0} - {1} [{2}]", ex.Source, ex, ex.Message);
End Try

Open in new window

-saige-

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.