Click here to Skip to main content
14,298,693 members
Rate this:
Please Sign up or sign in to vote.
See more:
hi.
I am totally new to visual studio.

My Code show below.

ports System.IO
Imports System.Data
Imports System.Data.OleDb

Public Class ImportData1

    Private Sub ImportData1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        TxtFilePath.Text = "Place your file in D:\FileToImport Folder. File name should be MatRegFile.csv"

    End Sub

    Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click

        Me.Close()

    End Sub

    Private Sub btnImport_Click(sender As Object, e As EventArgs) Handles btnImport.Click

        Dim conn As New OleDbConnection(“Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Mihir\Documents\MiniAGAppDB.accdb;Persist Security Info=True”)
        Dim cmd As OleDbCommand
        Dim querryStr As String

        Try

            querryStr = "Select * InTo Temp1 From [Text;FMT=Delimited;Database=D:\FileToImport;HDR=Yes].[MatRegFile.csv]"
            MessageBox.Show(querryStr)
            cmd = New OleDbCommand(querryStr, conn)
            conn.Open()
            cmd.ExecuteNonQuery()
            conn.Close()

            querryStr = "Insert InTo MaterialRegister ([Company_Code],[Company_Name],[Material_Code],[Material_Name],[Credit_Qty],[Debit_Qty],[Material_Count],[Material_Flag],[Material_Date],[Material_Inv_No],[Material_Party_Code],[Material_Party_Name],[Remark],[Entry_Flag],[Creation_Date],[User_Name]) Select [Company_Code],[Company_Name],[Material_Code],[Material_Name],[Credit_Qty],[Debit_Qty],[Material_Count],[Material_Flag],[Material_Date],[Material_Inv_No],[Material_Party_Code],[Material_Party_Name],[Remark],[Entry_Flag],[Creation_Date],[User_Name] From Temp1"
            cmd = New OleDbCommand(querryStr, conn)
            conn.Open()
            cmd.ExecuteNonQuery()
            conn.Close()

            querryStr = "Drop Table Temp1"
            cmd = New OleDbCommand(querryStr, conn)
            conn.Open()
            cmd.ExecuteNonQuery()
            conn.Close()

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        End Try

    End Sub

End Class


When I run the windows form application I get following error message at line
cmd.ExecuteNonQuery() code below "Insert Into MaterialRegister"


"
Visual studio 2017 System.AccessViolationException 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'
"

I am using windows 7 ultimate 64 bit edition with SP1, Visual Studio 2017 and .Net framework 4.6 with MS access database and Crystal report.

Please help......

What I have tried:

I don't know what to try as I am totally new to visual studio.
Posted
Updated 3-Sep-19 22:42pm

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

It is most likely that the previous SELECT INTO command did not create the data that you expect. You need to check the return value from each call to ExecuteNonQuery to see that it succeeded.
   
Comments
MKP75 4-Sep-19 4:54am
   
Thanks for the reply. The Select into Temp1 statement creates the Temp1 Table in access database and the data is also inserted into Temp1 table from CSV file.

Then it comes to Insert into MaterialRegister statement after that the error is thrown on ExecuteNonQuery Statement.

Actually the code worked for first 2 times then it started giving the error.
Richard MacCutchan 4-Sep-19 5:05am
   
OK, but the only way to find out why the error occurs is to use the debugger to check the data that you are processing. And, as I said earlier, check the return values from your system/library calls. Do not assume that just because they do not throw an exception that they have done what you think.
MKP75 4-Sep-19 5:16am
   
Dim querryStr1 As String
MKP75 4-Sep-19 5:15am
   
Thanks I will do that and get back. Thanks
MKP75 6-Sep-19 3:10am
   
This is what I get when I debug the program

'MiniAGApp.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MiniAGApp.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'C:\Users\Mihir\Documents\Visual Studio 2017\Projects\MiniAGApp\MiniAGApp\bin\Debug\MiniAGApp.exe'. Symbols loaded.
'MiniAGApp.exe' (CLR v4.0.30319: MiniAGApp.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Microsoft.VisualBasic\v4.0_10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualBasic.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MiniAGApp.exe' (CLR v4.0.30319: MiniAGApp.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MiniAGApp.exe' (CLR v4.0.30319: MiniAGApp.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MiniAGApp.exe' (CLR v4.0.30319: MiniAGApp.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MiniAGApp.exe' (CLR v4.0.30319: MiniAGApp.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Drawing\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MiniAGApp.exe' (CLR v4.0.30319: MiniAGApp.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Runtime.Remoting\v4.0_4.0.0.0__b77a5c561934e089\System.Runtime.Remoting.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MiniAGApp.exe' (CLR v4.0.30319: MiniAGApp.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\System.Data\v4.0_4.0.0.0__b77a5c561934e089\System.Data.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MiniAGApp.exe' (CLR v4.0.30319: MiniAGApp.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MiniAGApp.exe' (CLR v4.0.30319: MiniAGApp.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Data.DataSetExtensions\v4.0_4.0.0.0__b77a5c561934e089\System.Data.DataSetExtensions.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MiniAGApp.exe' (CLR v4.0.30319: MiniAGApp.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Numerics\v4.0_4.0.0.0__b77a5c561934e089\System.Numerics.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MiniAGApp.exe' (CLR v4.0.30319: MiniAGApp.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MiniAGApp.exe' (CLR v4.0.30319: MiniAGApp.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\System.Transactions\v4.0_4.0.0.0__b77a5c561934e089\System.Transactions.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MiniAGApp.exe' (CLR v4.0.30319: MiniAGApp.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\System.EnterpriseServices\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MiniAGApp.exe' (CLR v4.0.30319: Min
Richard MacCutchan 6-Sep-19 3:18am
   
That is not debug information. You need to set a breakpoint in your code and step through to see what data is being processed at each step, and what is the result of each of the system calls.
MKP75 6-Sep-19 3:46am
   
I have put break point on 4 lines that is querrystr, cmd = New OleDbCommand(querryStr, conn), conn.Open() and cmd.ExecuteNonQuery()

Below is what I get

Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at System.Data.Common.UnsafeNativeMethods.ICommandText.Execute(IntPtr pUnkOuter, Guid& riid, tagDBPARAMS pDBParams, IntPtr& pcRowsAffected, Object& ppRowset)
at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
at System.Data.OleDb.OleDbCommand.ExecuteNonQuery()
at MiniAGApp.ImportData1.btnImport_Click(Object sender, EventArgs e) in C:\Users\Mihir\Documents\Visual Studio 2017\Projects\MiniAGApp\MiniAGApp\Form14.vb:line 92
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at MiniAGApp.My.MyApplication.Main(String[] Args) in :line 81
The program '[6108] MiniAGApp.exe' has exited with code 0 (0x0).
Richard MacCutchan 6-Sep-19 6:45am
   
The problem is obviously happening before it hits the breakpoint so you need to stop it earlier.
MKP75 7-Sep-19 4:22am
   
hi. I tried debugging each and every line of code of that particular form. There are no errors or exception thrown except for

Visual studio 2017 System.AccessViolationException 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'

on cmd.ExecuteNonQuery() statement.

I don't understand this.

I even tried to insert data from DataGridview1 one by one but same error
occurs on cmd.ExecuteNonQuery line.

Is there any other way to do this. Please advice. I am stuck.
Richard MacCutchan 7-Sep-19 4:29am
   
Sorry, but without your code and all the data it is impossible. And, as I have said more than once, you need to look at the variables that are being used by the code. Access violations are caused by trying to read or write from an invalid memory address, and that usually means a bad parameter value passed in to the system call. But only you can identify which value that is.

And please try to stop posting your comments twice. If the system is slow it does not work better by reposting.
MKP75 7-Sep-19 4:37am
   
Ok. Thanks a lot.
MKP75 7-Sep-19 6:30am
   
Hi.

Problem solved. As you said I kept looking deeper into the program and nailed it.

Below is the working program for your reference.


Imports System.IO
Imports System.Data
Imports System.Data.OleDb

Public Class ImportData1

Dim conn As New OleDbConnection(“Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Mihir\Documents\MiniAGAppDB.accdb;Persist Security Info=True”)
Dim cmd As OleDbCommand
Dim querryStr1 As String
Dim querryStr2 As String
Dim querryStr3 As String

Private Sub ImportData1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

TxtFilePath.Text = "Place your file in D:\FileToImport Folder. File name should be MatRegFile.csv"

End Sub

Private Sub BtnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click

Me.Close()

End Sub

Private Sub BtnShow_Click(sender As Object, e As EventArgs) Handles btnShow.Click

Try

conn.Open()
Dim dbSchema As DataTable = conn.GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, "Temp1", "TABLE"})
conn.Close()

MessageBox.Show(dbSchema.Rows.Count)

If dbSchema.Rows.Count > 0 Then

querryStr1 = "Drop Table Temp1"
cmd = New OleDbCommand(querryStr1, conn)
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()

querryStr2 = "Select * InTo Temp1 From [Text;FMT=Delimited;Database=D:\FileToImport;HDR=Yes].[MatRegFile.csv]"
cmd = New OleDbCommand(querryStr2, conn)
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()

Else

querryStr2 = "Select * InTo Temp1 From [Text;FMT=Delimited;Database=D:\FileToImport;HDR=Yes].[MatRegFile.csv]"
cmd = New OleDbCommand(querryStr2, conn)
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()

End If

'Show Data From Temp1 In Data Grid
Dim objda As OleDbDataAdapter = New OleDbDataAdapter
Dim con As New OleDb.OleDbConnection(“Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Mihir\Documents\MiniAGAppDB.accdb;Persist Security Info=True”)
Dim objdataset As DataSet = New DataSet
Dim dv As DataView
con.Open()
objda.SelectCommand = New OleDbCommand("Select * From Temp1", con)
objda.Fill(objdataset, "Temp1")
dv = New DataView(objdataset.Tables("Temp1"))
DataGridView1.DataSource = dv
con.Close()
con.Dispose()

Catch ex As Exception

MessageBox.Show(ex.Message)
conn.Close()

End Try


End Sub

Private Sub btnImport_Click(sender As Object, e As EventArgs) Handles btnImport.Click

conn.Open()
Dim dbSchema As DataTable = conn.GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, "Temp1", "TABLE"})
conn.Close()

Try

conn.Open()
For Each rw As DataGridViewRow In DataGridView1.Rows
If rw.Cells(1).Value <> "" Then
cmd = New OleDbCommand("Insert Into MaterialRegister (Company_Code,Company_Name,Material_Code,Material_Name,Credit_Qty,Debit_Qty,Material_Count,Material_Flag,Material_Date,Material_Inv_No,Material_Party_Code,Material_Party_Name,Remark,Entry_Flag,Creation_Date,User_Name) values ('" & rw.Cells(1).Value & "','" & rw.Cells(2).Value & "','" & rw.Cells(3).Value & "','" & rw.Cells(4).Value & "','" & rw.Cells(5).Value & "','" & rw.Cells(6).Value & "','" & rw.Cells(7).Value & "','" & rw.Cells(8).Value & "','" & rw.Cells(9).Value & "','" & rw.Cells(10).Value &am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100