G
Guest
Hi
I need to validate the duplication of username.
Here is the aspx code
[code
<td>User Name:</td><td><asp:textbox id="username" Runat="server"></asp:textbox><asp:requiredfieldvalidator id="RequiredFieldValidatorUserName" runat="server" ErrorMessage="Please input user name!
Display="None" ControlToValidate="username" EnableViewState="False"></asp:requiredfieldvalidator><asp:customvalidator id="CustomValidatorUserName" runat="server" ErrorMessage="Your login name was taken by other, please re-enter!" Display="Dynamic" ControlToValidate="username" EnableClientScript="False"></asp:customvalidator><asp:validationsummary id="ValidationSummary" runat="server" ShowMessageBox="True" ShowSummary="False"></asp:validationsummary
[/code
Here is C# code
[code
private void InitializeComponent(
{
this.CustomValidatorUserName.ServerValidate += new System.Web.UI.WebControls.ServerValidateEventHandler(this.CustomValidatorUserName_ServerValidate)
this.btnSubmit.Click += new System.EventHandler(this.btnSubmit_Click)
this.Load += new System.EventHandler(this.Page_Load)
private void CustomValidatorUserName_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args
string userName
tr
userName = args.Value.Trim()
if(!Bool method to connect to DB with SQL to validate the existence of username
args.IsValid = true
return
catch(Exception) {
args.IsValid = false
[/code
The Bool method contains the sql: select username from usertable where username ='userName
The error is that the ErrorMessage="Your login name was taken by other, please re-enter!" cannot be displayed.
And, the duplicated username data insert to MS SQL 2000 DB.
What is wrong with my code?
Thanks for advice
I need to validate the duplication of username.
Here is the aspx code
[code
<td>User Name:</td><td><asp:textbox id="username" Runat="server"></asp:textbox><asp:requiredfieldvalidator id="RequiredFieldValidatorUserName" runat="server" ErrorMessage="Please input user name!
Display="None" ControlToValidate="username" EnableViewState="False"></asp:requiredfieldvalidator><asp:customvalidator id="CustomValidatorUserName" runat="server" ErrorMessage="Your login name was taken by other, please re-enter!" Display="Dynamic" ControlToValidate="username" EnableClientScript="False"></asp:customvalidator><asp:validationsummary id="ValidationSummary" runat="server" ShowMessageBox="True" ShowSummary="False"></asp:validationsummary
[/code
Here is C# code
[code
private void InitializeComponent(
{
this.CustomValidatorUserName.ServerValidate += new System.Web.UI.WebControls.ServerValidateEventHandler(this.CustomValidatorUserName_ServerValidate)
this.btnSubmit.Click += new System.EventHandler(this.btnSubmit_Click)
this.Load += new System.EventHandler(this.Page_Load)
private void CustomValidatorUserName_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args
string userName
tr
userName = args.Value.Trim()
if(!Bool method to connect to DB with SQL to validate the existence of username
args.IsValid = true
return
catch(Exception) {
args.IsValid = false
[/code
The Bool method contains the sql: select username from usertable where username ='userName
The error is that the ErrorMessage="Your login name was taken by other, please re-enter!" cannot be displayed.
And, the duplicated username data insert to MS SQL 2000 DB.
What is wrong with my code?
Thanks for advice