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

    Problemi con UPDATE in VBNET

    File .aspx:

    <%@ Page Language="VB" Inherits="Selezione" src="Selezione_Cliente.vb" %>

    <html>
    <head>
    </head>
    <body>
    <form method="post" runat="server">



    Seleziona un cliente:
    <aspropDownList id="lstclienti" runat="server" Width="139px" AutoPostBack="True"></aspropDownList>

    <asp:Button id="cmdupdate" runat="server" Text="Update"></asp:Button>

    <asp:Button id="cmddelete" runat="server" Text="Delete"></asp:Button>




    ID Univoco:
    <asp:Textbox id="txtid" runat="server"></asp:Textbox>


    Nome: <asp:Textbox id="txtnome" runat="server"></asp:Textbox>


    Cognome: <asp:Textbox id="txtcognome" runat="server"></asp:Textbox>


    Indirizzo: <asp:Textbox id="txtindirizzo" runat="server" Width="195px"></asp:Textbox>


    Citta':
    <asp:Textbox id="txtcitta" runat="server"></asp:Textbox>
    </p>



    <asp:Label id="lblerr" runat="server"></asp:Label>
    </p>
    </form>
    </body>
    </html>

    File vb.net (code behind):

    Imports Microsoft.VisualBasic
    Imports System
    Imports System.Web
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    Imports System.Data
    Imports System.Data.OleDb


    Public Class Selezione
    Inherits Page

    Protected withevents lstclienti as DropDownList
    Protected txtid as TextBox
    Protected txtnome as TextBox
    Protected txtcognome as TextBox
    Protected txtindirizzo as TextBox
    Protected txtcitta as TextBox
    Protected lblerr as Label
    Protected withevents cmdupdate as Button
    Protected withevents cmddelete as Button

    Private Sub Page_Load (sender as Object, e as EventArgs) Handles MyBase.Load

    if Me.IsPostBack = False Then
    Compilazione()
    End if

    End Sub

    Private function Compilazione()

    lstclienti.Items.Clear()


    Dim conn as New OleDbConnection()
    conn.ConnectionString="Provider=Microsoft.Jet.OLED B.4.0;" & "Data Source=f:/Esercizi_ASPNET/Database/clienti.mdb"

    Dim cmd as New OleDbCommand()
    cmd.Connection = conn
    cmd.CommandText="SELECT NOME, COGNOME, ID FROM clienti"

    Dim reader as OleDbDataReader

    Try

    conn.Open()
    reader=cmd.ExecuteReader()

    Do While reader.Read()

    Dim Item as New ListItem()
    Item.Text=reader("NOME") & ", " & reader("COGNOME")
    Item.Value=reader("ID")
    lstclienti.Items.Add(Item)

    Loop

    reader.Close()

    Catch err as Exception

    lblerr.Text = "ATTENZIONE: Errore nella lettura dei dati. "
    lblerr.Text &= err.Message

    Finally

    if (Not conn is Nothing) Then
    conn.close()
    end if

    End try
    End function

    Private sub lstclienti_SelectedIndexChanged (sender as Object, e as EventArgs) Handles lstclienti.SelectedIndexChanged

    Dim conn as New OleDbConnection()
    conn.ConnectionString="Provider=Microsoft.Jet.OLED B.4.0;" & "Data Source=f:/Esercizi_ASPNET/Database/clienti.mdb"

    Dim cmd as New OleDbCommand()
    cmd.Connection = conn
    cmd.CommandText="SELECT * FROM clienti WHERE ID=" & lstClienti.SelectedItem.Value & ""

    Dim reader as OleDbDataReader

    Try

    conn.Open()
    reader=cmd.ExecuteReader()

    reader.Read()

    txtid.Text = reader("ID")
    txtnome.Text = reader("NOME")
    txtcognome.Text = reader("COGNOME")
    txtindirizzo.Text = reader("INDIRIZZO")
    txtcitta.Text = reader("CITTA")

    reader.Close()

    Catch err as Exception

    lblerr.Text = "ATTENZIONE: Errore nella visualizzazione dei dati. "
    lblerr.Text &= err.Message

    Finally

    if (Not conn is Nothing) Then
    conn.close()
    end if

    End try

    End Sub

    Private sub cmdupdate_Click (sender as object, e as EventArgs) Handles cmdupdate.Click

    Dim UpdateSQL as String
    UpdateSQL = "UPDATE clienti SET NOME='txtnome.Text', COGNOME='txtcognome.Text', INDIRIZZO='txtindirizzo.Text', CITTA='txtcitta.Text' WHERE ID='lstclienti.SelectedItem.Value'"
    ' UpdateSQL &= "ID='" & txtid.Text & "', "
    'UpdateSQL &= "NOME='" & txtnome.Text & "', "
    'UpdateSQL &= "COGNOME='" & txtcognome.Text & "', "
    'UpdateSQL &= "INDIRIZZO='" & txtindirizzo.Text & "', "
    'UpdateSQL &= "CITTA='" & txtcitta.Text & "', "
    'UpdateSQL &= "WHERE ID='" & lstclienti.SelectedItem.Value & "'"


    Dim conn as New OleDbConnection()
    conn.ConnectionString="Provider=Microsoft.Jet.OLED B.4.0;" & "Data Source=f:/Esercizi_ASPNET/Database/clienti.mdb"
    dim cmd as New OleDbCommand(UpdateSQL, conn)

    Try

    conn.Open()
    dim updated as Integer
    updated=cmd.ExecuteNonQuery
    lblerr.Text= updated.ToString() & " records aggiornati."

    Catch err as Exception

    lblerr.Text = "ATTENZIONE: Errore nell'aggiornamento dei dati. "
    lblerr.Text &= err.Message

    Finally

    if (Not conn is Nothing) Then
    conn.close()
    end if

    End try

    End Sub
    End Class

    Quando avvio il file .aspx, seleziono all'interno della combobox il dato e lo vado a modificare. Mi rileva un errore di sintassi all'interno del seguente codice:

    Private sub cmdupdate_Click (sender as object, e as EventArgs) Handles cmdupdate.Click

    Dim UpdateSQL as String
    UpdateSQL = "UPDATE clienti SET NOME='txtnome.Text', COGNOME='txtcognome.Text', INDIRIZZO='txtindirizzo.Text', CITTA='txtcitta.Text' WHERE ID='lstclienti.SelectedItem.Value'"
    ' UpdateSQL &= "ID='" & txtid.Text & "', "
    'UpdateSQL &= "NOME='" & txtnome.Text & "', "
    'UpdateSQL &= "COGNOME='" & txtcognome.Text & "', "
    'UpdateSQL &= "INDIRIZZO='" & txtindirizzo.Text & "', "
    'UpdateSQL &= "CITTA='" & txtcitta.Text & "', "
    'UpdateSQL &= "WHERE ID='" & lstclienti.SelectedItem.Value & "'"

    Vi prego provatelo con calma e fatemi sapere.

    Grazie in anticipo

    P.S.: Per il caro moderatore: Io la domanda l'ho posta in modo gentile, se poi non riuscite a capirmi questo è un problema vostro. Comunque, se qualcuno ti postasse inutilmente non ti darebbe fastidio? Non ti farebbe perdere del tempo? Inoltre se davvero volessi fare il moderatore cerca di mettere pace e non di rivolgermi tutte quelle inutili domande.

  2. #2
    "UPDATE clienti SET NOME='" & txtnome.Text & "', COGNOME='" & txtcognome.Text & "', INDIRIZZO='" & txtindirizzo.Text & "', CITTA='" & txtcitta.Text & "' WHERE ID='" & lstclienti.SelectedItem.Value & "'"


    questa sintassi è corretta.
    Ora prova a controllare che non passi valori vuoti. Se lo fai e ti va bene di passare valori vuoti, vai su Access e cambia le proprietà del campo in questione. Mettendo Richiesto su NO e Consenti lunghezza zero su SI.

    Poi il campo ID sei sicuro che sia di tipo testo e non numerico?

    Se è un contatore non devi metterlo nella UPDATE come hai fatto tu.

    Cmq in access se vai su query puoi riscriverti la query e passargli alcuni valori e vedere il suo corretto funzionamento.


    ciao

  3. #3
    Utente di HTML.it
    Registrato dal
    Aug 2001
    Messaggi
    1,482
    Carissimo g.basurto questo è il codice che avevi postato tu:

    codice:
    updateSQL as string 
    updateSQL="UPDATE(tabella) SET " 
    updateSQL&="ID='" & txtid.Text & "'" 
    updateSQL&="NOME='" & txtnome.Text & "'" 
    ... 
    updateSQL&="WHERE ID=" & lstclienti.selecteditem.Value & "
    Questo è quello che ti aveva scritto Innovatel

    codice:
    dim updateSQL as string 
    updateSQL  = " UPDATE(tabella) SET " 
    updateSQL& = " ID='" & txtid.Text & "' , "  '<-----guarda bene la virgola
    updateSQL& = " NOME='" & txtnome.Text & "' "
    Come vedi, cosa non da poco mancava una virgola

    ..
    innovatel, pensavo che mi avresti dato un aiuto !! Cerca di non postare inutilmente.

    Grazie
    ma non ci arrivi
    era mio quel messaggio. Di suo non ha dato proprio un bel niente.
    Comunque cercate di non postare inutilmente. Odio le persone che mi fanno perdere tempo.
    Se questo ti sembra essere gentile.

    Sperando che sia l'ultima volta chiudo.

    Hey hey, my my Rock and roll can never die!

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.