si... hai ragione...
allora posto tutto il codice...
codice:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'CONNESSIONE DATABASE
Dim PercorsoDB As String = "C:\ANA\DB\DB_ANA.mdb"
'Stringa di Connessione
Dim ConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Persist Security Info=False;" & _
"Data Source=" & PercorsoDB & _
";" & _
"User ID=Admin;" & _
"Jet OLEDB:Database Password=pippo;"
'Fine Stringa di Connessione
Dim Cn As New OleDb.OleDbConnection(ConnString)
Dim sql, sql1 As String
'RECUPERO I VALORI DA UN DATAGRID PER INSERIRLI SUCCESSIVAMENTE SUL DATABASE
Dim numerorow As Integer
'INIZIA IL CICLO
For numerorow = 0 To DataGridView1.RowCount - 1
Dim c_cognome, c_nome, c_sesso, c_gruppo As String
Dim c_data As Date
Cn.Open()
c_cognome = DataGridView1.Item("COGNOME", numerorow).Value.ToString
c_nome = DataGridView1.Item("NOME", numerorow).Value.ToString
c_data = DataGridView1.Item("DATA_NASCITA", numerorow).Value.ToString
c_sesso = DataGridView1.Item("SESSO", numerorow).Value.ToString
c_gruppo = VAL_GRUPPOComboBox.Text
'EFFETTUO UNA QUERY PER VEDERE SUCCESSIVAMENTE SE NELLA TABELLA DEL DATABASE
'ESISTE GIà UN RECORD CON COGNOME NOME E DATA UGUALE
sql1 = "SELECT * FROM Person WHERE COGNOME = " & _
c_cognome & _
"; SELECT * FROM Person WHERE COGNOME = " & _
c_cognome & _
" and NOME = " & _
c_nome & _
" and DATA = " & _
c_data
'RECUPERO IL NUMERO DEI RECORD RESTITUITI DALLA QUERY
Dim cmd1 As New OleDb.OleDbCommand(sql1, Cn)
Dim records As Integer
records = cmd1.ExecuteScalar
If records = 0 Then
'INSERISCO I DATI DEL DATAGRID NELLA TABELLA DEL DATABASE
sql = "INSERT INTO Person (COGNOME , NOME , DATA , SESSO , GRUPPO ) VALUES ('" + c_cognome + "', '" + c_nome + "', '" + c_data + "', '" + c_sesso + "', '" + c_gruppo + "')"
Dim cmd As New OleDb.OleDbCommand(sql, Cn)
Dim temp_num As Integer
cmd = New OleDb.OleDbCommand(sql, Cn)
MsgBox("Aggiunto nuovo affiliato: " & c_cognome & " " & c_nome & " " & c_data & " ", , "Informazione su Nuovo Affiliato")
temp_num = cmd.ExecuteNonQuery
Cn.Close()
Else
'RICEVO UN MESSAGGIO DI ERRORE
MsgBox("ATTENZIONE, Soggetto già presente!")
Cn.Close()
End If
Next
'RIPRENDE IL CICLO
End Sub
L'errore che mi restituisce è sulla riga
codice:
records = cmd1.ExecuteScalar
ed il messaggio di errore è il seguente:
codice:
System.Data.OleDb.OleDbException non è stata gestita
ErrorCode=-2147217900
Message="Caratteri non previsti dopo la fine dell'istruzione SQL."
Source="Microsoft JET Database Engine"
StackTrace:
in System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
in System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
in System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
in System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
in System.Data.OleDb.OleDbCommand.ExecuteScalar()
in anagrafe.FormCoscheAffNucl.Button1_Click(Object sender, EventArgs e) in C:\Users\Francesco\Documents\Visual_Studio_2005\Projects\anagrafe\anagrafe\anagrafe\FormCoscheAffNucl.vb:riga 123
in System.Windows.Forms.Control.OnClick(EventArgs e)
in System.Windows.Forms.Button.OnClick(EventArgs e)
in System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
in System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
in System.Windows.Forms.Control.WndProc(Message& m)
in System.Windows.Forms.ButtonBase.WndProc(Message& m)
in System.Windows.Forms.Button.WndProc(Message& m)
in System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
in System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
in System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
in System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
in System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
in System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
in System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
in System.Windows.Forms.Application.Run(ApplicationContext context)
in Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
in Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
in Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
in anagrafe.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:riga 81