Home Page
Articoli
Tips & Tricks
News
Forum
Archivio Forum
Blogs
Sondaggi
Rss
Video
Utenti
Chi Siamo
Contattaci
Username:
Password:
Login
Registrati ora!
Recupera Password
Home Page
Stanze Forum
ASP.NET 2.0 / 3.5 / 4.0
Creare un customvalidator per verificare che la chiave sia rispettata
martedì 30 maggio 2006 - 14.22
Elenco Threads
Stanze Forum
Aggiungi ai Preferiti
Cerca nel forum
event
Profilo
| Newbie
14
messaggi | Data Invio:
mar 30 mag 2006 - 14:22
Non so se è chiaro il problema da topic...
allora devo mettere un validatore che scriva all'utente(in postback) che l'email inserità è gia presente nel DB(xchè email è chiave...)
In realtà ora mi da un pagina di errore semplice in cui dice che la chiave deve essere unica...
quindi il custom validator dovrebbe andare a cercare nella tabella: tbl_utente se esiste gia un email, nel campo "email", uguale a quello immesso dall'utente nell'emailTextbox, il tutto prima che mi dia l'errore. Stampando ovviamente un messaggio di errore accanto alla texbox(come gli altri validatori...)
Grazie.
GvnnRules
Profilo
| Senior Member
436
messaggi | Data Invio:
mar 30 mag 2006 - 14:37
Puoi usare la proprietà OnServerValidate, indicando la funzione che deve fare il controllo, quà trovi un esempio:
http://www.asp.net/QuickStart/util/srcview.aspx?path=~/aspnet/samples/validation/CustomValidator.src
Peace
Gvnn
http://www.gvnn.it
http://www.sqlwebarchitect.org
http://blogs.dotnethell.it/gvnn/
aabruzzese
Profilo
| Junior Member
89
messaggi | Data Invio:
mar 30 mag 2006 - 16:34
Ciao, O travoto un essempio e lo cambiato per fare quello che cerci tu: Ce un Stored Proc e una file .vb e una .aspx.
CREATE PROCEDURE sp_CheckForDuplicates
(
@UserName Email(50) = NULL,
@Duplicates INT = 0
)
AS
SET @Duplicates =(SELECT COUNT(*) FROM NorthWindUsers
WHERE Email = @Email)
RETURN @Duplicates
--------------------------------------------------------------------------------------------------------
Imports System.Web.Security ' |||||| Required Class for Authentication
Imports System.Data ' |||||| DB Accessing Import
Imports System.Data.SqlClient ' |||||| SQL Server Import
Imports System.Configuration ' |||||| Required for Web.Config appSettings |||||
Public Class CheckDupEmail
Inherits System.Web.UI.Page
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
Private Sub cmdSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSubmit.Click
If Page.IsValid Then ' Meaning the Control Validation was successful!
' All fields have been filled in!
If ValidateEmail(txtEmail.Text.Trim(), Then
' The Email does not exist
End If
End If
End Sub
Function ValidateEmail(ByVal strAlias As String) As Boolean
' <summary>
' ||||| This function simply verifies that there is no existing match on
' ||||| Email (alias), and that the email does not already registered!
' </sumary>
' ||||| Set up a Connection Object to the SQL DB
Dim MyConn As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("strConn"))
' ||||| Pass in the StoreProcedure or Command String, as well as the Connection object
Dim MyCmd As New SqlCommand("sp_CheckForDuplicates", MyConn)
' ||||| Set the Command Type (Stored Procedure, Text, etc)
MyCmd.CommandType = CommandType.StoredProcedure
' ||||| Create Parameter Objects for values passed in
Dim objParam1, objParam2, objParam3 As SqlParameter
' ||||| Create a parameter to store your Return Value from the Stored Procedure
Dim objReturnParam As SqlParameter
' ||||| Add your parameters to the parameters Collection
objParam1 = MyCmd.Parameters.Add("@Email", SqlDbType.VarChar)
objReturnParam = MyCmd.Parameters.Add("@Duplicates", SqlDbType.Int)
objReturnParam.Direction = ParameterDirection.ReturnValue
' ||||| Set the Parameter values to the passed in values
objParam1.Value = strAlias
Try
' ||||| Check if Connection to DB is already open, if not, then open a connection
If MyConn.State = ConnectionState.Closed Then
' ||||| DB not already Open...so open it
MyConn.Open()
MyCmd.ExecuteNonQuery()
End If
' ||||| Was the return value greater than 0 ???
If objReturnParam.Value > 0 Then
lblResult.Text = "Email already exists!"
Return False
Else
Return True
End If
' ||||| Close the Connection Closes with it
MyConn.Close()
Catch ex As Exception
lblError.Text = "Error Connecting to Database!"
End Try
End Function
End Class
----------------------------------------------------------------------------------------------------------------------------
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="CheckDupEmail.aspx.vb" Inherits="CheckDupEmail"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Register</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="
http://schemas.microsoft.com/intellisense/ie5">
</head>
<body ms_positioning="GridLayout">
<h1>Welcome Email Duplicate Checker Page</h1>
<form id="frmRegister" method="post" runat="server">
<table id="tblRegister" border="1" cellpadding="2" cellspacing="1">
<tr>
<td style="WIDTH: 108px">User Name:</td>
<td><asp:textbox id="txtEmail" runat="server"></asp:textbox><font color="#ff3300" size="4"><strong>*</strong></font></td>
<td><asp:requiredfieldvalidator id="rvEmail" runat="server" errormessage="<-- Required" controltovalidate="txtEmail"></asp:requiredfieldvalidator></td>
</tr
<tr>
<td colspan="2" align="center"><asp:button id="cmdSubmit" text="Submit" runat="server"></asp:button>
</td>
</tr>
</table>
</form>
<p><font color="#ff3300" size="4">*</font> Required Field
<asp:label id="lblError" style="Z-INDEX: 101; LEFT: 656px; POSITION: absolute; TOP: 264px"
runat="server" width="216px"></asp:label></p>
<p> </p>
<p> </p>
<asp:label id="lblResult" runat="server" width="424px"></asp:label>
</body>
</html>
Angelo Abruzzese
event
Profilo
| Newbie
14
messaggi | Data Invio:
mer 31 mag 2006 - 02:25
ah ok grazie
Torna su
Stanze Forum
Elenco Threads
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?
Dopo esserti registrato potrai chiedere
aiuto sul nostro
Forum
oppure aiutare gli altri
Consulta le
Stanze
disponibili.
Registrati ora !