[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 550
  • Last Modified:

"Attempted to read or write protected memory vb.net error pls help

Hi Experts,

I have encountered the below error while running my vb.net console application . Pls help

"Attempted to read or write protected memory. This is often an indication
that other memory is corrupt"

0
nyee84
Asked:
nyee84
  • 3
  • 2
1 Solution
 
Melih SARICACommented:
send the code pls..
0
 
nyee84Author Commented:
Imports System.Object
Imports System.IO
Imports System.Text
Imports System.Data.SqlClient
Imports System.Data.Odbc
Imports Microsoft.VisualBasic
Imports System.Diagnostics


Module Module1
    Const KeyExist = "c:\Download\KeyExist\"
    Const DuplicateKey = "\DuplicateKey\"
     Const TextFilepath = "C:\Download\sourcefile\"
    Const LogFilepath = "c:"
    'Const TextFilepath = "c:\New Folder"
    Const WrongFormatKey = "C:\Download\wrongsourceformat\"
     Private objFileStream As StreamWriter
    Private strLogMsg As String

    Sub MAIN()
        'Added 2006.02.13
        'Create the necessary file

        Dim strLogFileName As String = "log" + Format(Now, "yyyy") + Format(Now, "MM") + Format(Now, "dd") & Format(Now, "HHmm") & ".txt"


        If (File.Exists(TextFilepath & "\" & strLogFileName)) Then
            File.Delete(TextFilepath & "\" & strLogFileName)
        End If
        objFileStream = File.CreateText(LogFilepath & "\" & strLogFileName)

        Dim TempDir As DirectoryInfo = New DirectoryInfo(TextFilepath)
        If TempDir.GetFiles("*.txt").Length <= 0 Then
            strLogMsg = "No Text File Found"
            Console.WriteLine(strLogMsg)
            objFileStream.WriteLine(strLogMsg)
            'Console.Read()
        Else
            strLogMsg = "Processing  Source Text Files"
            Console.WriteLine(strLogMsg)
            objFileStream.WriteLine(strLogMsg)

            'ReadTextFiles(TextFilepath.ToString(), WrongFormatKey.ToString(), DuplicateKey.ToString(), KeyExist.ToString())
        End If

        If TempDir.GetFiles("*.xls").Length <= 0 Then
            strLogMsg = "No XLS  File Found"
            Console.WriteLine(strLogMsg)
            objFileStream.WriteLine(strLogMsg)

            'Console.Read()
        Else
            strLogMsg = "Processing Apple Source Excel Files"
            Console.WriteLine(strLogMsg)
            objFileStream.WriteLine(strLogMsg)

            ReadXLSfiles(TextFilepath.ToString(), DuplicateKey.ToString(), KeyExist.ToString(), WrongFormatKey.ToString())
        End If

        strLogMsg = "Completed"
        Console.WriteLine(strLogMsg)
        objFileStream.WriteLine(strLogMsg)

        objFileStream.Close()
        'Console.Read()
    End Sub

    Private Sub WriteToLog(ByVal strMessage As String, ByVal TextFilepath As String)

        Dim objFS As FileStream = New FileStream(TextFilepath, FileMode.Append, FileAccess.Write)
        Dim objSW As StreamWriter = New StreamWriter(objFS)
        objSW.WriteLine(strMessage)
        objSW.Close()
        objFS.Close()

    End Sub

   
    Private Sub ReadXLSfiles(ByVal pTextFilepath As String, ByVal pDuplicateKey As String, ByVal pKeyExist As String, ByVal pWrongFormatKey As String)
        Try
            Dim strRD As StreamReader
            Dim conxls As OleDb.OleDbConnection
            Dim txtxlsflag As Integer = 0
            Dim TempDir As DirectoryInfo = New DirectoryInfo(pTextFilepath)

            Dim strLine As String
            Dim SQLCon As SqlConnection = New SqlConnection("Server=(local);integrated security=true;Database=app")
            Dim SQLTRAN As SqlTransaction
            Dim SQLcmd As SqlCommand = New SqlCommand
            Dim RowsAffected As Integer
            Dim StartFromRow As Integer = 1
            Dim strOldsno As String = String.Empty
            SQLCon.Open()
            SQLTRAN = SQLCon.BeginTransaction("TRAN")
            SQLcmd.Connection = SQLCon
            SQLcmd.Transaction = SQLTRAN

            For xfile As Integer = 0 To TempDir.GetFiles("*.xls").Length - 1
                Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & TempDir.GetFiles("*.xls")(0).FullName & ";Extended Properties=""Excel 8.0;HDR=NO"";"
                '                                       "Data Source=" & TempDir.GetFiles("*.xls")(xfile).FullName & ";Extended Properties=""Excel 8.0;HDR=NO"""
                conxls = New OleDb.OleDbConnection(ConnectionString)
                conxls.Open()

                Dim fname As String = TempDir.GetFiles("*.xls")(0).Name

                strLogMsg = "Processing " & fname 'st
                Console.WriteLine(strLogMsg)
                objFileStream.WriteLine(strLogMsg)

                '                fname = Left(fname, fname.Length - 3) + "xls"

                'fname = "c:\New Folder\testingPID.xls"
                'Dim da As New System.Data.OleDb.OleDbDataAdapter("Select * from [" + fname + "]", conxls)

                ''get sheet name
                Dim oExcel As New Excel.Application
                Dim oBooks As Excel.Workbooks, oBook As Excel.Workbook
                Dim oSheet As Excel.Worksheet
                Dim oCells As Excel.Range
                Dim strsheetname As String
                Dim strSQL As String

                oExcel.Visible = False : oExcel.DisplayAlerts = False

                oBooks = oExcel.Workbooks
                'oBooks.Open(TempDir.GetFiles("*.xls")(xfile).FullName)
                If File.Exists("c:\" + fname) Then
                    File.Delete("c:\" + fname)
                End If

                File.Copy(TempDir.GetFiles("*.xls")(0).FullName, "c:\" & fname)
                oBooks.Open("c:\" & fname)
                oBook = oBooks.Item(1)
                oSheet = oExcel.Sheets.Item(1)
                strsheetname = oSheet.Name
                oExcel.Quit()
                oExcel = Nothing

                strSQL = "Select * from [ " & strsheetname & "$]"
                'Dim da As New System.Data.OleDb.OleDbDataAdapter("Select * from " & strsheetname, conxls)
                Dim da As New System.Data.OleDb.OleDbDataAdapter("Select * from [" + strsheetname + "$]", conxls)

                Dim dt As New DataTable
                da.Fill(dt)
                If dt.Columns.Count <> 3 And dt.Columns.Count <> 1 Then
                    conxls.Close() 'st
                    GC.Collect()
                    If Not IsNothing(strRD) Then strRD.Close()
                    'File.Copy(TempDir.GetFiles("*.xls")(xfile).FullName, "C:\Download\wrongsourceformat\" & TempDir.GetFiles("*.txt")(xfile).Name, True)
                    'File.Delete(TempDir.GetFiles("*.txt")(xfile).FullName)
                    Dim pwe As String = pWrongFormatKey '+ TempDir.GetFiles("*.xls")(xfile).Name
                    Dim ssew As String = TempDir.GetFiles("*.xls")(0).FullName
                    Dim ss1ew As String
                    ss1ew = Right(ssew, ssew.Length - TextFilepath.Length)
                    'ss1 = Left(ss, InStrRev(ss, "\"))
                    If File.Exists(pwe + ss1ew) Then
                        File.Delete(pwe + ss1ew)
                    End If
                    Dim sourcepath As String = TempDir.GetFiles("*.xls")(0).FullName
                    File.Move(sourcepath, pwe + ss1ew)

                    strLogMsg = fname & "wrong format. File moved to \\wrongsourceformat folder" 'st
                    Console.WriteLine(strLogMsg)
                    objFileStream.WriteLine(strLogMsg)

                    If Not IsNothing(strRD) Then strRD.Close() 'st

                    'File.Delete(TempDir.GetFiles("*.txt")(xfile).FullName)
                ElseIf dt.Columns.Count = 3 Then 'st//duplicate checking
                    Dim dr As DataRow
                    Dim x As Integer

                    x = 0

                    For Each dr In dt.Rows

                        strLine = CType(dr(2), String)
                        If (CheckXlsDuplicate(strLine, dt, dt.Columns.Count)) Then
                            'If strOldsno = strLine Then
                            SQLTRAN.Rollback()
                            Dim pt As String = pDuplicateKey '+ TempDir.GetFiles("*.xls")(xfile).Name
                            Dim ss As String = TempDir.GetFiles("*.xls")(0).FullName
                            Dim ss1 As String
                            ss1 = Right(ss, ss.Length - TextFilepath.Length)
                            'ss1 = Left(ss, InStrRev(ss, "\"))
                            If File.Exists(pt + ss1) Then
                                File.Delete(pt + ss1)
                            End If
                            File.Copy(ss, pt + ss1)

                            'File.Copy(TempDir.GetFiles("*.xls")(xfile).FullName, pt, True)
                            'File.Delete(TempDir.GetFiles("*.xls")(xfile).FullName)
                            Exit Sub
                        End If

                        'SQLcmd.CommandText = "If not Exists(Select * from source Where SerialNo ='" & CType(dr(2), String) & "')   " & _
                        '                  " Insert into source values ('" & CType(dr(2), String) & "')"

                        SQLcmd.CommandText = "Select count(*)  as cnt from source Where SerialNo ='" & CType(dr(2), String) & "' "
                        SQLcmd.CommandType = CommandType.Text
                        RowsAffected = SQLcmd.ExecuteScalar()

                        If RowsAffected > 0 And x = 0 Then
                            'SQLTRAN.Rollback()

                            Try
                                Dim pxe As String = pKeyExist '+ TempDir.GetFiles("*.xls")(xfile).Name
                                Dim sse As String = TempDir.GetFiles("*.xls")(0).FullName
                                Dim ss1e As String
                                ss1e = Right(sse, sse.Length - TextFilepath.Length)
                                'ss1 = Left(ss, InStrRev(ss, "\"))
                                If File.Exists(pxe + ss1e) Then
                                    File.Delete(pxe + ss1e)
                                End If
                                Dim sourcepath As String = TempDir.GetFiles("*.xls")(0).FullName

                                File.Copy(sse, pxe + ss1e)

                                If x = 0 Then
                                    conxls.Close()
                                    conxls = Nothing
                                End If

                                File.Delete(TempDir.GetFiles("*.xls")(0).FullName)

                            Catch ex As Exception

                            End Try


                           

                            x += 1



                            'Return
                            'File.Delete(TempDir.GetFiles("*.xls")(xfile).FullName)
                        Else

                            SQLcmd.CommandText = "Insert into source values ('" & CType(dr(2), String) & "') "
                            SQLcmd.CommandType = CommandType.Text
                            RowsAffected = SQLcmd.ExecuteNonQuery()

                        End If

                        'y = +1
                        'If y < dt.Rows.Count() Then
                        '    SQLTRAN = SQLCon.BeginTransaction("TRAN")
                        '    SQLcmd.Connection = SQLCon
                        '    SQLcmd.Transaction = SQLTRAN
                        'End If
                    Next
                    dr = Nothing

                    da = Nothing
                    conxls.Close() '
                    conxls = Nothing

                    File.Delete(TempDir.GetFiles("*.xls")(0).FullName)

                    Console.WriteLine(TempDir.GetFiles("*.xls")(0).FullName)
                    ' File.Delete(TempDir.GetFiles("*.xls")(xfile).FullName)

                Else  ''''Only 1 column

                    Dim X As Integer = 0
                    Dim dr As DataRow
                    For Each dr In dt.Rows
                        strLine = CType(dr(0), String)
                        If (CheckXlsDuplicate1(strLine, dt)) Then
                            'If strOldsno = strLine Then
                            SQLTRAN.Rollback()
                            Dim pt As String = pDuplicateKey '+ TempDir.GetFiles("*.xls")(xfile).Name
                            Dim ss As String = TempDir.GetFiles("*.xls")(0).FullName
                            Dim ss1 As String
                            ss1 = Right(ss, ss.Length - TextFilepath.Length)
                            'ss1 = Left(ss, InStrRev(ss, "\"))
                            If File.Exists(pt + ss1) Then
                                File.Delete(pt + ss1)
                            End If
                            File.Copy(ss, pt + ss1)

                            'File.Copy(TempDir.GetFiles("*.xls")(xfile).FullName, pt, True)
                            'File.Delete(TempDir.GetFiles("*.xls")(xfile).FullName)
                            Exit Sub
                        End If
                        SQLcmd.CommandText = "If not Exists(Select * from applesource Where SerialNo ='" & CType(dr(0), String) & "')   " & _
                                          " Insert into applesource values ('" & CType(dr(0), String) & "')"
                        SQLcmd.CommandType = CommandType.Text
                        RowsAffected = SQLcmd.ExecuteNonQuery()

                        If RowsAffected = -1 Then
                            SQLTRAN.Rollback()

                            Dim pxe As String = pKeyExist '+ TempDir.GetFiles("*.xls")(xfile).Name
                            Try

                                Dim sse As String = TempDir.GetFiles("*.xls")(0).FullName
                                Dim ss1e As String
                                ss1e = Right(sse, sse.Length - TextFilepath.Length)
                                'ss1 = Left(ss, InStrRev(ss, "\"))
                                If File.Exists(pxe + ss1e) Then
                                    File.Delete(pxe + ss1e)
                                End If
                                Dim sourcepath As String = TempDir.GetFiles("*.xls")(0).FullName

                                File.Copy(sse, pxe + ss1e)

                                File.Delete(TempDir.GetFiles("*.xls")(0).FullName)


                            Catch ex As Exception

                            End Try

                            If x = 0 Then
                                conxls.Close()
                                conxls = Nothing
                            End If

                            x += 1



                            'Return
                            'File.Delete(TempDir.GetFiles("*.xls")(xfile).FullName)
                        End If
                    Next
                    dr = Nothing

                    da = Nothing

                    conxls.Close() '
                    conxls = Nothing

                    File.Delete(TempDir.GetFiles("*.xls")(0).FullName)

                    Console.WriteLine(TempDir.GetFiles("*.xls")(0).FullName)
                    ' File.Delete(TempDir.GetFiles("*.xls")(xfile).FullName)

                    ''''''''''''''''''''''''''''''''''
                    ''''End for 1 column
                    ''''''''''''''''''''''''''''''''''

                End If
            Next
            SQLTRAN.Commit()
            SQLCon.Close()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub
0
 
nyee84Author Commented:
The issue already been resolved....
0
 
Melih SARICACommented:
In which line of code u got the error.. Code Seems ok...

What i think is maybe Stream operations can cause this error

0
 
nyee84Author Commented:
.net frame work 1.1 ver not installed, were using 2.0 instead
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now