Click here to Skip to main content
15,897,273 members

Insert records into table after checking the duplicate values

saradhiAD asked:

Open original thread
Hi, this is saradhi.
I am inserting the records from datagrid view(these fields are loaded from one excel file) into Access data table without inserting duplicate values.
Here is my code:
MIDL
Dim ConStr As String = "Provider=Microsoft.JET.OLEDB.4.0;Data Source= D:\DB.mdb"
Dim Con As New OleDbConnection(ConStr)
Dim cmd1 As New OleDbCommand
Con.Open()
cmd1.CommandType = CommandType.Text
"IF NOT EXISTS( SELECT * FROM [Table] where Col1 = '" & Col1 & "')" & _
        " begin insert into [Table](Col1,Col2,Col3,Col4,Col5) " & _
        "values('" & Col1 & "','" & Col2 & "','" & Col3e & "','" & Col4 & "','" & Col5 & "') End"
cmd1.Parameters.Add("@Col1", OleDbType.VarChar, 50).Value = Col1(These assigned(col1,col2,..etc.,) valus will be come frm another func)
cmd1.Parameters.Add("@Col2", OleDbType.VarChar, 50).Value = Col2
cmd1.Parameters.Add("@Col3", OleDbType.VarChar, 50).Value = Col3
cmd1.Parameters.Add("@Col4", OleDbType.VarChar, 50).Value = Col4
cmd1.Parameters.Add("@Col5", OleDbType.VarChar, 50).Value = Col5        
cmd1.ExecuteReader()
Con.Close()


When I run the code the error is:
System.InvalidOperation Exception
{"ExecuteReader: Connection property has not been initialized."}


Then somebody told me that I have to use object to create command.
Then I code used like below:
Dim cmd1 As New OleDbCommand  = Con.CreateCommand

Now the error is:
Invalid Statement error:expected 'insert,delete,select,update,procedure'

So please help me for this. urgent
Tags: Visual Basic

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900