Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    [VB.NET]Apertura nuovo form a aggiornamento dati database

    Salve ragazzi buona giornata.

    Dopo avere effettuato una ricerca mostro i dati in un DataGrid che ho nekl form1.
    Ad ogni riga ho tre pulsanti che devono cancellare, inserire e eliminare il record.
    Queste tre operazioni vengono effettuate in un nuovo form.
    Per esempio per cancellare il prodotto, ho detto al pulsante di aprire il FORM 4 e di riepilogare in campi testo i dati da cancellare. Alla fine ho un pulsante che mi esegue la query sul FORM 4. I dati nei campi testo del FORM 4 li visualizzo dopo aver creato un DataGrid e li filtro tramite id.

    Il problema è che quando eseguo la query per la cancellazione del record i dati non mi vengono cacellate dal db e io non ricevo nessun errore.

    Ecco il codice che apre il FORM4:
    codice:
    If e.ColumnIndex = Me.DataGridResultRicerca.Columns("btnElimina").Index Then
                Form4.Visible = True
                Dim idDettaglio As Integer
                idDettaglio = CType(DataGridResultRicerca.CurrentRow.Cells(3).Value, String)
    
                Dim DataConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\magazzino.mdb;"
                Dim cn As New OleDbConnection()
                cn.ConnectionString = DataConnString
                cn.Open()
    
                Dim sql As String = "Select * from dettaglio WHERE id_dettaglio = " & idDettaglio & " Order By id_dettaglio"
    
                Dim cmd As New OleDbCommand()
    
                cmd.Connection = cn
                cmd.CommandText = sql
    
                Dim dad As OleDbDataAdapter = New OleDbDataAdapter(cmd)
                Dim dataset As DataSet = New DataSet("dettaglio")
    
                dad.Fill(dataset)
    
                Form4.DataGridGestioneProdotto.DataSource = dataset
                Form4.DataGridGestioneProdotto.DataMember = dataset.Tables(0).TableName
                Form4.DataGridGestioneProdotto.Visible = False
    
                If Not Form4.DataGridGestioneProdotto.Rows(Form4.DataGridGestioneProdotto.CurrentRow.Index).Cells(0).Value Is System.DBNull.Value Then
                    Form4.txtIdDettaglio.Text = Form4.DataGridGestioneProdotto.Rows(Form4.DataGridGestioneProdotto.CurrentRow.Index).Cells(0).Value
                End If
                If Not Form4.DataGridGestioneProdotto.Rows(Form4.DataGridGestioneProdotto.CurrentRow.Index).Cells(1).Value Is System.DBNull.Value Then
                    Form4.txtSerialNumber.Text = Form4.DataGridGestioneProdotto.Rows(Form4.DataGridGestioneProdotto.CurrentRow.Index).Cells(1).Value
                End If
                If Not Form4.DataGridGestioneProdotto.Rows(Form4.DataGridGestioneProdotto.CurrentRow.Index).Cells(3).Value Is System.DBNull.Value Then
                    Form4.txtTipoArticolo.Text = Form4.DataGridGestioneProdotto.Rows(Form4.DataGridGestioneProdotto.CurrentRow.Index).Cells(3).Value
                End If
                If Not Form4.DataGridGestioneProdotto.Rows(Form4.DataGridGestioneProdotto.CurrentRow.Index).Cells(5).Value Is System.DBNull.Value Then
                    Form4.txtCodArticolo.Text = Form4.DataGridGestioneProdotto.Rows(Form4.DataGridGestioneProdotto.CurrentRow.Index).Cells(5).Value
                End If
                If Not Form4.DataGridGestioneProdotto.Rows(Form4.DataGridGestioneProdotto.CurrentRow.Index).Cells(9).Value Is System.DBNull.Value Then
                    Form4.txtFornitore.Text = Form4.DataGridGestioneProdotto.Rows(Form4.DataGridGestioneProdotto.CurrentRow.Index).Cells(9).Value
                End If
                If Not Form4.DataGridGestioneProdotto.Rows(Form4.DataGridGestioneProdotto.CurrentRow.Index).Cells(11).Value Is System.DBNull.Value Then
                    Form4.txtDataArrivo.Text = Form4.DataGridGestioneProdotto.Rows(Form4.DataGridGestioneProdotto.CurrentRow.Index).Cells(11).Value
                End If
                If Not Form4.DataGridGestioneProdotto.Rows(Form4.DataGridGestioneProdotto.CurrentRow.Index).Cells(4).Value Is System.DBNull.Value Then
                    Form4.txtMarca.Text = Form4.DataGridGestioneProdotto.Rows(Form4.DataGridGestioneProdotto.CurrentRow.Index).Cells(4).Value
                End If
                If Not Form4.DataGridGestioneProdotto.Rows(Form4.DataGridGestioneProdotto.CurrentRow.Index).Cells(6).Value Is System.DBNull.Value Then
                    Form4.txtDescrizionearticolo.Text = Form4.DataGridGestioneProdotto.Rows(Form4.DataGridGestioneProdotto.CurrentRow.Index).Cells(6).Value
                End If
            End If
    questo è il codice di aggiornamento nel form4:
    codice:
       Private Sub btnCancellaArticolo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancellaArticolo.Click
            Dim DataConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\magazzino.mdb;"
            Dim cn As New OleDbConnection()
            cn.ConnectionString = DataConnString
            cn.Open()
            Dim sql As String = "DELETE * FROM dettaglio WHERE id_dettaglio " & Me.txtIdDettaglio.Text & ""
            Dim cmd As New OleDbCommand()
    
            cmd.Connection = cn
            cmd.CommandText = sql
    
          cn.Close()
    
        End Sub
    Da cosa può dipendere?

    Grazie a tutti.
    <a href="http://www.robertodidonato.it" target="_blank">Roberto Di Donato</a>

  2. #2
    Utente di HTML.it L'avatar di gibra
    Registrato dal
    Apr 2008
    residenza
    Italy
    Messaggi
    4,244
    Forse dal fatto che non ricarichi la griglia dopo la cancellazione?

    Che senso ha caricare i dati in un form per cancellarli?


  3. #3
    Dunque allora per quanto riguarda l'operazione di cancellazione prima postata ho scoperto che è un'errore di Sql e ho risolto.

    Però per quanto riguarda aggiornamento ed inserimento record mi succede una cosa strana.

    Se provo a inserire alcuni campi come per esempio la "dataVendita" non procede con l'inserimento senza però andare in errore.

    Una volta effettuata l'operazione proveddo al refresh del datagrid che si trova nel FORM 1 dove vengono mostrati i risultati delle ricerche.

    Ora non capisco perchè alcuni campi del DB mi generano questo problema.

    <a href="http://www.robertodidonato.it" target="_blank">Roberto Di Donato</a>

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.