WTsuk
asked on
Visual Studio 2019: CSV import work on DEBUG but does not work in RELEASE
I am a beginner. I spent 3 days with the problem here. I'd appreciate it if you could help me.
I am using Visual Studio 2019 vb.net
the import file is comma-delimited CSV file
user navigate and select the CSV file from local pc into the textbox.
CSV file:
Jobno,SCF
C2322,194
C2322,161
C2322,195
C2322,197
Problem:
CSV file import works in debug mode but does not work in Release mode.
Code:
Dim fstream As System.IO.FileStream
objCon = objCMDSCon.GetConnection
If IO.File.Exists(txtCSVFilePath.Text.Trim) = False Then
errmsg = "Chosen file does not exist."
flag = False
retval = 1 'cancel
e.Cancel = True
Return retval
End If
'To check whether CSV file is already opened or not
fstream = IO.File.OpenRead(txtCSVFilePath.Text.Trim)
fstream.Flush()
fstream.Close()
strConString = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + System.IO.Path.GetDirectoryName(txtCSVFilePath.Text) + ";"
objOdbcConn = New Odbc.OdbcConnection(strConString)
daCSV = New System.Data.Odbc.OdbcDataAdapter("select * from [" + System.IO.Path.GetFileName(txtCSVFilePath.Text) + "]", objOdbcConn)
dsCSV.Clear()
daCSV.Fill(dsCSV) 'Shoot Error here
Dim tempcheckfile() As String
Dim ext As Integer
tempcheckfile = Split(IO.File.OpenRead(txtCSVFilePath.Text.Trim).Name, ".", 3)
ext = tempcheckfile.GetUpperBound(0)
If tempcheckfile(ext).ToLower <> "csv" Then
errmsg = "Chosen file is not compatible for import."
flag = False
retval = 1 'cancel
e.Cancel = True
Return retval
End If
If dsCSV.Tables(0).Columns.Count <> 2 Then
errmsg = "Chosen file is not compatible for import."
flag = False
retval = 1 'cancel
e.Cancel = True
Return retval
ElseIf dsCSV.Tables(0).Rows.Count <= 0 Then
errmsg = "No Valid Data found for Import."
flag = False
retval = 1 'cancel
e.Cancel = True
Return retval
End If
I am using Visual Studio 2019 vb.net
the import file is comma-delimited CSV file
user navigate and select the CSV file from local pc into the textbox.
CSV file:
Jobno,SCF
C2322,194
C2322,161
C2322,195
C2322,197
Problem:
CSV file import works in debug mode but does not work in Release mode.
Code:
Dim fstream As System.IO.FileStream
objCon = objCMDSCon.GetConnection
If IO.File.Exists(txtCSVFilePath.Text.Trim) = False Then
errmsg = "Chosen file does not exist."
flag = False
retval = 1 'cancel
e.Cancel = True
Return retval
End If
'To check whether CSV file is already opened or not
fstream = IO.File.OpenRead(txtCSVFilePath.Text.Trim)
fstream.Flush()
fstream.Close()
strConString = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + System.IO.Path.GetDirectoryName(txtCSVFilePath.Text) + ";"
objOdbcConn = New Odbc.OdbcConnection(strConString)
daCSV = New System.Data.Odbc.OdbcDataAdapter("select * from [" + System.IO.Path.GetFileName(txtCSVFilePath.Text) + "]", objOdbcConn)
dsCSV.Clear()
daCSV.Fill(dsCSV) 'Shoot Error here
Dim tempcheckfile() As String
Dim ext As Integer
tempcheckfile = Split(IO.File.OpenRead(txtCSVFilePath.Text.Trim).Name, ".", 3)
ext = tempcheckfile.GetUpperBound(0)
If tempcheckfile(ext).ToLower <> "csv" Then
errmsg = "Chosen file is not compatible for import."
flag = False
retval = 1 'cancel
e.Cancel = True
Return retval
End If
If dsCSV.Tables(0).Columns.Count <> 2 Then
errmsg = "Chosen file is not compatible for import."
flag = False
retval = 1 'cancel
e.Cancel = True
Return retval
ElseIf dsCSV.Tables(0).Rows.Count <= 0 Then
errmsg = "No Valid Data found for Import."
flag = False
retval = 1 'cancel
e.Cancel = True
Return retval
End If
ASKER
Yes. it is Windows From VB.NET project.
Because I don't see anything inherently wrong with your code. My only thought was you had hard coded the text file to test your code but failed to copy the text file to the release location. Visual Studio will add a Debug and Release folder to the bin folder in order to separate out the two builds.
-saige-
-saige-
I'll try recreating your issue. Give me a few minutes.
-saige-
-saige-
are you targeting the same platform (ie x86) in both Debug and Release?
ASKER
I do not know what is the "platform (ie x86) " means.
Right next to the Debug/Release drop down is another drop down. It could contain x86, x64 or Any CPU.
-saige-
-saige-
ASKER
Company hired to build the project and unfortunately not completed. I am trying to complete it.
ASKER
it is AnyCPU.
ASKER
Debug : right click on Project name and selected Compile. The Target CPU is x86. Platform is Active (Any CPU)
Release : right click on Project name and selected Compile. The Target CPU is AnyCPU. Platform is Active (Any CPU)
Release : right click on Project name and selected Compile. The Target CPU is AnyCPU. Platform is Active (Any CPU)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you so much. it works. Import CSV file into datagrid. I changed x86 in both Debug and Release. I really appreciate it.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
what is the difference between x86 and AnyCPU? Could both be changed to AnyCPU?
Oh and incase you were wondering. There is a much simpler way to write that code.
Here is mine:
Form1.vb -
Happy Coding!!!
-saige-
Here is mine:
Form1.vb -
Imports System.Data.Odbc
Imports System.IO
Public Class Form1
Private Sub OnClick(sender As Object, e As EventArgs) Handles ActionChoose.Click
If FileDialog.ShowDialog() = DialogResult.OK Then
Dim file = New FileInfo(FileDialog.FileName)
Dim table As New DataTable()
Dim connectionString = $"Driver={{Microsoft Text Driver (*.txt; *.csv)}};Dbq={file.Directory};"
Dim query = $"SELECT * FROM [{file.Name}]"
Using connection = New OdbcConnection(connectionString)
Using adapter = New OdbcDataAdapter(query, connection)
adapter.Fill(table)
JobNumbers.DataSource = table
End Using
End Using
End If
End Sub
End Class
Form1.Designer.vb -<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
Partial Class Form1
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()>
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()>
Private Sub InitializeComponent()
Me.FileDialog = New System.Windows.Forms.OpenFileDialog()
Me.ActionChoose = New System.Windows.Forms.Button()
Me.SplitContainer1 = New System.Windows.Forms.SplitContainer()
Me.JobNumbers = New System.Windows.Forms.DataGridView()
CType(Me.SplitContainer1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SplitContainer1.Panel1.SuspendLayout()
Me.SplitContainer1.Panel2.SuspendLayout()
Me.SplitContainer1.SuspendLayout()
CType(Me.JobNumbers, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'FileDialog
'
Me.FileDialog.DefaultExt = "csv"
Me.FileDialog.Filter = "CSV|*.csv"
'
'ActionChoose
'
Me.ActionChoose.Location = New System.Drawing.Point(3, 3)
Me.ActionChoose.Name = "ActionChoose"
Me.ActionChoose.Size = New System.Drawing.Size(75, 23)
Me.ActionChoose.TabIndex = 0
Me.ActionChoose.Text = "Choose File"
Me.ActionChoose.UseVisualStyleBackColor = True
'
'SplitContainer1
'
Me.SplitContainer1.Dock = System.Windows.Forms.DockStyle.Fill
Me.SplitContainer1.Location = New System.Drawing.Point(0, 0)
Me.SplitContainer1.Name = "SplitContainer1"
Me.SplitContainer1.Orientation = System.Windows.Forms.Orientation.Horizontal
'
'SplitContainer1.Panel1
'
Me.SplitContainer1.Panel1.Controls.Add(Me.ActionChoose)
'
'SplitContainer1.Panel2
'
Me.SplitContainer1.Panel2.Controls.Add(Me.JobNumbers)
Me.SplitContainer1.Size = New System.Drawing.Size(234, 450)
Me.SplitContainer1.SplitterDistance = 31
Me.SplitContainer1.TabIndex = 1
'
'JobNumbers
'
Me.JobNumbers.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.AllCells
Me.JobNumbers.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.AllCells
Me.JobNumbers.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
Me.JobNumbers.Dock = System.Windows.Forms.DockStyle.Fill
Me.JobNumbers.Location = New System.Drawing.Point(0, 0)
Me.JobNumbers.MultiSelect = False
Me.JobNumbers.Name = "JobNumbers"
Me.JobNumbers.ReadOnly = True
Me.JobNumbers.RowHeadersVisible = False
Me.JobNumbers.Size = New System.Drawing.Size(234, 415)
Me.JobNumbers.TabIndex = 0
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(234, 450)
Me.Controls.Add(Me.SplitContainer1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.SplitContainer1.Panel1.ResumeLayout(False)
Me.SplitContainer1.Panel2.ResumeLayout(False)
CType(Me.SplitContainer1, System.ComponentModel.ISupportInitialize).EndInit()
Me.SplitContainer1.ResumeLayout(False)
CType(Me.JobNumbers, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
Friend WithEvents FileDialog As OpenFileDialog
Friend WithEvents ActionChoose As Button
Friend WithEvents SplitContainer1 As SplitContainer
Friend WithEvents JobNumbers As DataGridView
End Class
Produces the following output -Happy Coding!!!
-saige-
ASKER
Thanks for the coding. I copied it.
>>what is the difference between x86 and AnyCPU? Could both be changed to AnyCPU?
AnyCPU will favor x64 if the CPU supports it. The problem that you have is that you are using ODBC which is surely relying on 32-bits connectors that are not running correctly in x64.
AnyCPU will favor x64 if the CPU supports it. The problem that you have is that you are using ODBC which is surely relying on 32-bits connectors that are not running correctly in x64.
ASKER
My pc and server are 64 bit. THE user PC can be 32 bit. will x86 work for 64 bit and 32 bit?
yes it will
ASKER
Thank you so much.
-saige-