Visualizzazione dei risultati da 1 a 6 su 6
  1. #1
    Utente di HTML.it
    Registrato dal
    Mar 2010
    Messaggi
    44

    [VB net]inserimento dopo aver selezionato dei checkBox

    ragazzi avevo gia postato nell'altro post,ma giustamente meglio parlarne in un altro post.
    allora ho in una form una serie di checkbox,ho possibilità di selezionarli e una volta fatta questa operazione clicco su salva e vorrei inserire nel database tanti record quanti sono i check selezionati,posto il codice che fin ora ho fatto...è un pò bruttino lo so ma non sapevo come fare .
    codice:
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim ObjConnection As OleDbConnection
            Dim ObjCommand As OleDbCommand
            Dim ObjCommand2 As OleDbCommand
            Dim StrConn As String
            Dim Query As String
            Dim ID As Integer
            Dim RispUtente As Integer
            Try
                StrConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Progetto Carrara\Carrara\Carrara\DbCarrara.accdb;"
                ObjConnection = New OleDbConnection(StrConn)
                ObjConnection.Open()
    
                'RECUPERO L'Ultimo Id inserito
                '"SELECT TOP 1 * FROM Cliente ORDER BY IDcliente DESC"
    
                ObjCommand = New OleDbCommand("SELECT TOP 1 * FROM Cliente ORDER BY IDcliente DESC", ObjConnection)
                ID = ObjCommand.ExecuteScalar()
                ObjCommand2 = New OleDbCommand("INSERT INTO CLIENTE_SERVIZIO (CLIENTE,SERVIZIO) VALUES (?,?)", ObjConnection)
                If (CheckBox1.Checked = True) Then
                    ObjCommand.Parameters.Add("@CLIENTE", OleDbType.Integer).Value = ID
                    ObjCommand.Parameters.Add("@SERVIZIO", OleDbType.Integer).Value = 1
                    RispUtente = ObjCommand2.ExecuteNonQuery()
                End If
                '    If (CheckBox2.Checked = True) Then
                'ObjCommand.Parameters.Add("@CLIENTE", OleDbType.Integer).Value = ID
                '  ObjCommand.Parameters.Add("@SERVIZIO", OleDbType.Integer).Value = 2
                '  RispUtente = ObjCommand2.ExecuteNonQuery()
                '  End If
                '  If (CheckBox3.Checked = True) Then
                ' ObjCommand.Parameters.Add("@CLIENTE", OleDbType.Integer).Value = ID
                'ObjCommand.Parameters.Add("@SERVIZIO", OleDbType.Integer).Value = 3
                ' RispUtente = ObjCommand2.ExecuteNonQuery()
    
                '  End If
                ' If (CheckBox4.Checked = True) Then
                'ObjCommand.Parameters.Add("@CLIENTE", OleDbType.Integer).Value = ID
                ' ObjCommand.Parameters.Add("@SERVIZIO", OleDbType.Integer).Value = 4
                ' RispUtente = ObjCommand2.ExecuteNonQuery()
                'End If
                ' If (CheckBox5.Checked = True) Then
                'ObjCommand.Parameters.Add("@CLIENTE", OleDbType.Integer).Value = ID
                ' ObjCommand.Parameters.Add("@SERVIZIO", OleDbType.Integer).Value = 5
                'RispUtente = ObjCommand2.ExecuteNonQuery()
    
                'End If
                'If (CheckBox6.Checked = True) Then
                'ObjCommand.Parameters.Add("@CLIENTE", OleDbType.Integer).Value = ID
                ' ObjCommand.Parameters.Add("@SERVIZIO", OleDbType.Integer).Value = 6
                'RispUtente = ObjCommand2.ExecuteNonQuery()
                'End If
                'If (CheckBox7.Checked = True) Then
                'ObjCommand.Parameters.Add("@CLIENTE", OleDbType.Integer).Value = ID
                ' ObjCommand.Parameters.Add("@SERVIZIO", OleDbType.Integer).Value = 7
                'RispUtente = ObjCommand2.ExecuteNonQuery()
                ' End If
                '  MsgBox(ID)
                ' If (CheckBox1.Checked = False And CheckBox2.Checked = False And CheckBox3.Checked = False And CheckBox4.Checked = False And CheckBox5.Checked = False And CheckBox6.Checked = False And CheckBox7.Checked = False) Then
                'MsgBox("Selezionare un Servizio")
                ' End If
                MsgBox(ID)
    
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    End Class
    mi da l'errore, Nessun valore specificato per alcuni parametri necessari...cioè?

  2. #2
    La prossima volta .... le righe commentate, non postarle, fanno solo confusione.
    Ad ogni modo ..... l'errore è di distrazione.
    Guarda bene a quale command stai aggiungendo i parametri.

    Facci sapere...
    Ciao

  3. #3
    Utente di HTML.it
    Registrato dal
    Mar 2010
    Messaggi
    44
    allora scusatemi le righe commentate sono considerate buone..nel senso non sono commenti...sono validi tutti quei IF,si grazie ho risolto ora aggiunge i record...ma come mai mi inserisce sempre lo stesso record?per esempio ID 10 e poi sono selezionati 3 checkbox,la 1,la 2 e la 3...mi inserisce sempre 10 - 1..perchè?

  4. #4
    Prova con
    codice:
    SELECT MAX(ID) FROM Cliente
    Anche se, non capisco perchè mai debba calcolartelo tu L'id, quando, di solito, si imposta un campo Contatore nella tabella di Access(e nel qual caso Max(ID) non andrebbe più bene.)

    Facci sapere...
    Ciao

  5. #5
    Utente di HTML.it
    Registrato dal
    Mar 2010
    Messaggi
    44
    allora l'ID va bene...perchè io precedentemente nell'altra form mi salvo i dati e questi hanno un determinato ID contatore,poi quando apro un'altra form mi serve l'ID appena inserito..e devo mettere in un altra tabella l'ID e i servizi richiesti...solo che cosi mi mette sempre il primo servizio selezionato...forsein ogni IF devo mettere Insert into...

  6. #6
    Per come hai impostato il codice, è normale che ti inserisc per tutti i Record lo stesso ID ... forse dovresti incrementarlo

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.