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

    controlli personalizzati e connessione che non si chiude

    Stò lavorando su di un controllo personalizzato per fare il LogIn. La mia pagina aspx è di questo tipo:

    codice:
    <%@ Page Language="VB" %>
    <%@ Register TagPrefix="myControls" Namespace="MyControls" Assembly="MyLogIn" %>
    <%@ Import namespace="System.Data.OleDb"%>
    <script runat="server">
    
        Sub CaricaDati (s as object, e as eventargs)
                  Dim ChkConn As OleDbConnection
                  Dim ChkCmd As OleDbCommand
                  Dim StrSql As string
                  Dim ChkData As OleDbDataReader
                  Dim strName As string
                  Dim StrPasswd As string
    
                  strName = CtrlLogIn.UserName
                  StrPasswd = CtrlLogIn.Password
                  ChkConn= New OleDbConnection(ConfigurationSettings.AppSettings("ConnForum"))
                  StrSql ="SELECT Is_Id, Is_Nome, Is_Password, Is_Level FROM Iscritti WHERE Is_Nome =? AND Is_Password =?"
                  ChkCmd= New OleDbCommand (strsql,ChkConn)
                  ChkCmd.Parameters.Add("@Name",strName)
                  ChkCmd.Parameters.Add("@Password",StrPasswd)
    
                  ChkConn.open()
                  ChkData=ChkCmd.ExecuteReader()
    
                  CtrlLogIn.DataSource=ChkData
             End Sub
    
    
    </script>
    <html>
    <head><title>DisplayLoginEvent.aspx</title></head>
    <body>
    <form Runat="Server">
    
    <MyControls:MyLogIn id="CtrlLogIn" OnLogIn="CaricaDati" Runat="Server"/>
    
    </form>
    </body>
    </html>
    La sub CaricaDati è associata all'evento OnLogIn che ho definito all'interno del mio controllo. Ora, dopo aver fatto CtrlLogIn.DataSource=ChkData vorrei chiudere reader e connessione, però se lo faccio mi restituisce questo errore:

    System.InvalidOperationException: Invalid attempt to FieldCount when reader is closed

    Sapete come posso fare?
    www.radiana.it - Spartiti musicali e Mp3

  2. #2
    Utente di HTML.it
    Registrato dal
    Aug 2001
    Messaggi
    1,482
    così ti da errore?

    codice:
                  ChkConn.open()
                  ChkData=ChkCmd.ExecuteReader()
                  CtrlLogIn.DataSource=ChkData
                  ChkConn.close()
    Se si allora penso ci sia qualcosa che non va nel tuo controllo personalizzato. Dovresti postare un po' di codice.

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

  3. #3
    Mi da errore. Ecco il codice del mio controllo:

    codice:
    Imports System
    Imports System.Web
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    Imports System.Collections
    
    Namespace MyControls
    
        Public Class MyLogIn
          Inherits Control
          Implements INamingContainer
    
          'Creo l'evento OnLogin
          Public Event LogIn (s As Object, e As EventArgs)
    
          Protected Sub OnLogIn( e As EventArgs )
            RaiseEvent LogIn( Me, EventArgs.Empty )
          End Sub
    
          'Creo le proprietà della classe
          Private _dataSource As IEnumerable
    
            Public Property DataSource As IEnumerable
              Get
                return _dataSource
              End Get
    
              Set
                _dataSource = value
              End Set
            End Property
    
            Public Property Username As String
              Get
                Me.EnsureChildControls()
                Return CType( Controls( 2 ), TextBox ).Text
              End Get
    
              Set
                Me.EnsureChildControls()
                CType( Controls( 2 ), TextBox ).Text = Value
              End Set
            End Property
    
            Public Property Password As String
              Get
                Me.EnsureChildControls()
                Return CType( Controls( 5 ), TextBox ).Text
              End Get
    
              Set
                Me.EnsureChildControls()
                CType( Controls( 5 ), TextBox ).Text = Value
              End Set
            End Property
    
            'Creo i metodi della classe
            Sub CheckPassword( s As Object, e As EventArgs )
    
              OnLogIn (EventArgs.Empty)
              
              Dim lblLabel As Label
              Dim DataEnum As IEnumerator
              lblLabel = CTYPE( Controls( 11 ), Label )
    
                If Not _dataSource Is Nothing
    
                  DataEnum = _dataSource.GetEnumerator()
                    if DataEnum.MoveNext()=false then
                      lblLabel.text="UserName o password sbagliate"
                    else
                      lblLabel.text="Bravo!!!"
                    end if
                End If
    
    
    
    
            End Sub
    
    
            Protected Overrides Sub CreateChildControls()
              Me.Controls.Add( New LiteralControl( "<table><tbody>" ))
    
                  Dim Username As new TextBox
                  Username.id="Username"
                  Me.Controls.Add( New LiteralControl( "<tr><td>UserName:</td><td>" ) )
                  Me.Controls.Add( Username )
                  Me.Controls.Add( New LiteralControl( "</td></tr>" ) )
    
                  Dim Password As New TextBox
                  Password.id="Password"
                  Password.TextMode = TextBoxMode.Password
                  Me.Controls.Add( New LiteralControl( "<tr><td>Password:</td><td>" ) )
                  Me.Controls.Add( Password )
                  Me.Controls.Add( New LiteralControl( "</td></tr>" ) )
    
                  Dim LogInButton As New Button
                  LogInButton.id="LogIn_Button"
                  LogInButton.text="LogIn"
                  AddHandler LogInButton.Click, AddressOf CheckPassword
                  Me.Controls.Add( New LiteralControl("<tr><td align=""middle"" colspan=""2"">"))
                  Me.Controls.Add( LogInButton )
                  Me.Controls.Add( New LiteralControl( "</td></tr>" ) )
    
    
                  Dim lblLabel As New Label
                  lblLabel.EnableViewState = False
                  Me.Controls.Add( New LiteralControl("<tr><td align=""middle"" colspan=""2"">"))
                  Me.Controls.Add( lblLabel )
                  Me.Controls.Add( New LiteralControl( "</td></tr>" ) )
    
       
    
              Me.Controls.Add( New LiteralControl( "</tbody></table>" ))
            End Sub
    
        End Class
    
    End Namespace
    Ho visto però che se modfico il codice in questa maniera:
    codice:
       ...
    
    'Creo i metodi della classe
            Sub CheckPassword( s As Object, e As EventArgs )
    
              OnLogIn (EventArgs.Empty)
    
            End Sub
    
            Protected Overrides Sub OnDataBinding( e As EventArgs )
    
              Dim lblLabel As Label
              Dim DataEnum As IEnumerator
              lblLabel = CTYPE( Controls( 11 ), Label )
    
                If Not _dataSource Is Nothing
    
                  DataEnum = _dataSource.GetEnumerator()
                    if DataEnum.MoveNext()=false then
                      lblLabel.text="UserName o password sbagliate"
                    else
                      lblLabel.text="Bravo!!!"
                    end if
                End If
    
            End Sub
    
       ...
    posso tranquillamente chiudere connessione e datareader (e forse è anche più corretto...). Solo non capisco perchè nell'altro modo mi da l'errore e così no :master:
    www.radiana.it - Spartiti musicali e Mp3

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 © 2026 vBulletin Solutions, Inc. All rights reserved.