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:
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