User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the ASP.NET section within the Web Development category of DaniWeb, a massive community of 396,884 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,311 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our ASP.NET advertiser: Lunarpages ASP Web Hosting

ASP.NET Registration Page

Join Date: Jul 2005
Posts: 2
Reputation: Farlie_76 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Farlie_76's Avatar
Farlie_76 Farlie_76 is offline Offline
Newbie Poster

Re: ASP.NET Registration Page

  #3  
Jul 22nd, 2005
Hi

I have tried to add more fields ie: Address, Phone, Email etc everything compiles ok but when running the app it comes up with the database error (lblError)

I have updated the Stored procedure and the database with the correct fields so i think i know it is not that.

any way here is codebehind:

Imports System.Web.Security ' |||| Required Class for Authentication
Imports System.Data ' |||| DB Accessing Import
Imports System.Data.SqlClient ' |||| SQL Server Import
Imports System.Configuration ' |||| Web.Config appsettings
Public Class register
	Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

	'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

	End Sub
	Protected WithEvents txtUserName As System.Web.UI.WebControls.TextBox
	Protected WithEvents rvUserName As System.Web.UI.WebControls.RequiredFieldValidator
	Protected WithEvents txtPassword As System.Web.UI.WebControls.TextBox
	Protected WithEvents rvPassword As System.Web.UI.WebControls.RequiredFieldValidator
	Protected WithEvents txtFirst As System.Web.UI.WebControls.TextBox
	Protected WithEvents rvFirstName As System.Web.UI.WebControls.RequiredFieldValidator
	Protected WithEvents txtLast As System.Web.UI.WebControls.TextBox
	Protected WithEvents rvLastName As System.Web.UI.WebControls.RequiredFieldValidator
	Protected WithEvents cmdSubmit As System.Web.UI.WebControls.Button
	Protected WithEvents lblError As System.Web.UI.WebControls.Label
	Protected WithEvents lblResult As System.Web.UI.WebControls.Label
	Protected WithEvents txtCompany As System.Web.UI.WebControls.TextBox
	Protected WithEvents rvCompany As System.Web.UI.WebControls.RequiredFieldValidator
	Protected WithEvents txtAddress As System.Web.UI.WebControls.TextBox
	Protected WithEvents rvAddress As System.Web.UI.WebControls.RequiredFieldValidator
	Protected WithEvents txtCity As System.Web.UI.WebControls.TextBox
	Protected WithEvents rvCity As System.Web.UI.WebControls.RequiredFieldValidator
	Protected WithEvents txtPostCode As System.Web.UI.WebControls.TextBox
	Protected WithEvents rvPostCode As System.Web.UI.WebControls.RequiredFieldValidator
	Protected WithEvents txtEmail As System.Web.UI.WebControls.TextBox
	Protected WithEvents rvEmail As System.Web.UI.WebControls.RequiredFieldValidator
	Protected WithEvents txtPhone As System.Web.UI.WebControls.TextBox
	Protected WithEvents rvPhone As System.Web.UI.WebControls.RequiredFieldValidator
	Protected WithEvents txtFax As System.Web.UI.WebControls.TextBox

	'NOTE: The following placeholder declaration is required by the Web Form Designer.
	'Do not delete or move it.
	Private designerPlaceholderDeclaration As System.Object

	Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
		'CODEGEN: This method call is required by the Web Form Designer
		'Do not modify it using the code editor.
		InitializeComponent()
	End Sub

#End Region

	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 Fileds have been filled in!
		    If ValidateNewUser(txtUserName.Text.Trim(), txtFirst.Text.Trim(), txtLast.Text.Trim()) Then
			    AddNewUser(txtUserName.Text.Trim(), txtFirst.Text.Trim(), txtLast.Text.Trim(), txtPassword.Text.Trim())
				Response.Redirect("login2.aspx")
			End If
		End If
	End Sub

	Function ValidateNewUser(ByVal strAlias As String, ByVal strFirst As String, ByVal strLast As String) As Boolean
		' |||| This function simply varifies that there is no existing match on
		' |||| username (alias), and that the user has not already registered!
		' |||| |||| |||| |||| |||| |||| |||| |||| |||| ||||
		' |||| Set up a connection object to the SQL DB
		Dim MyConn As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("strConn"))
		' |||| Pass in the StoredProcedure 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, objParam5 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("@UserName", SqlDbType.VarChar)
		objParam2 = MyCmd.Parameters.Add("@FirstName", SqlDbType.VarChar)
		objParam3 = MyCmd.Parameters.Add("@LastName", 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
		objParam2.Value = strFirst
		objParam3.Value = strLast


		Try
		    ' |||| Check if Connection to the 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 that 0?
		    If objReturnParam.Value > 0 Then
			    lblResult.Text = "UserName already exists or you are already a registered user!"
				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

	Sub AddNewUser(ByVal strUser As String, ByVal strFirst As String, ByVal strLast As String, ByVal strPass As String)
		' |||| Set up new Connection To the SQL DB
		Dim MyConn As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("strConn"))
		' |||| Pass in the StoredProcedure or Command String, as well as the Connection object
		Dim MyCmd As New SqlCommand("sp_RegisterNewUser", MyConn)
		' |||| Set the command type (Stored Procedure, Test, etc)
		MyCmd.CommandType = CommandType.StoredProcedure
		' |||| Create Parameter objects for values passed in
		Dim objParam1, objParam2, objPAram3, objParam4, objParam5, objParam6, objParam7, objParam8, objParam9, objParam10, objParam11 As SqlParameter
		' |||| Creat an 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("@UserName", SqlDbType.VarChar)
		objParam2 = MyCmd.Parameters.Add("@FirstName", SqlDbType.VarChar)
		objPAram3 = MyCmd.Parameters.Add("@LastName", SqlDbType.VarChar)
		objParam4 = MyCmd.Parameters.Add("@Password", SqlDbType.VarChar)
		objParam5 = MyCmd.Parameters.Add("@Company", SqlDbType.VarChar)
		objParam6 = MyCmd.Parameters.Add("@Address", SqlDbType.VarChar)
		objParam7 = MyCmd.Parameters.Add("@City", SqlDbType.VarChar)
		objParam8 = MyCmd.Parameters.Add("@PostCode", SqlDbType.VarChar)
		objParam9 = MyCmd.Parameters.Add("@Email", SqlDbType.VarChar)
		objPAram10 = MyCmd.Parameters.Add("@Phone", SqlDbType.VarChar)
		objParam11 = MyCmd.Parameters.Add("@Fax", SqlDbType.VarChar)
		' |||| Set the Parameter values to the passed in values
		objParam1.Value = strUser
		objParam2.Value = strFirst
		objPAram3.Value = strLast
		objParam4.Value = strPass

		Try
		    ' |||| Check if connection is already open
			MyConn.Open()
			MyCmd.ExecuteNonQuery()
			MyConn.Close()
		Catch ex As Exception
			lblError.Text = "Error Connecting to DataBase!"
		End Try
	End Sub
End Class
Last edited by Paladine : Jul 22nd, 2005 at 11:06 am. Reason: Adding code blocks
Reply With Quote  
All times are GMT -4. The time now is 3:57 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC