Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Use values from registration table to login

vb database login authentification registration

  • Please log in to reply
5 replies to this topic

#1 luffy mcawesome

luffy mcawesome

    CC Newcomer

  • Member
  • PipPip
  • 12 posts

Posted 24 October 2012 - 09:18 AM

Hi, I've already set up a local SQL database in VS 2010.. And i've already connected my registration web form with it.
So, whenever someone do a registration, the data values are automatically stored in the database when btnSubmit is Clicked...

My problem is that, I want to use the UserName value and the Password value from the Registration table to be Used as Login Credentials..

I'm trying not to use the login Controls but rather by using labels and Textboxes

My sql connection:

<connectionStrings>
   <add name="jjshopconn" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\JJS.mdf;Integrated Security=True;User Instance=True"
   providerName="System.Data.SqlClient" />
</connectionStrings>

Here's my code For The Registration aspx.vb:



Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration

Partial Class JJS_Register
    Inherits System.Web.UI.Page

    Private ReadOnly _conString As String

    Public Sub New()
	    _conString = WebConfigurationManager.ConnectionStrings("jjshopconn").ConnectionString
    End Sub
   
    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
	    Dim con As New SqlConnection(_conString)
	    Dim insertSql As String = "INSERT INTO tbl_Customer (Cust_fname,Cust_lname,Cust_uname,Cust_pwd,Cust_email,Cust_status)" & " values (@FirstName,@LastName,@UserName,@Password,@Email,@Status)"
	    Dim con1 As New SqlConnection(_conString)
	    Dim cmd As New SqlCommand()	    cmd.Connection = con
	    cmd.CommandType = CommandType.Text
	    cmd.CommandText = insertSql

	    Dim firstName As New SqlParameter("@FirstName", SqlDbType.VarChar, 50)
	    firstName.Value = txtFirstName.Text.ToString()
	    cmd.Parameters.Add(firstName)	    Dim lastName As New SqlParameter("@LastName", SqlDbType.VarChar, 50)
	    lastName.Value = txtLastName.Text.ToString()
	    cmd.Parameters.Add(lastName)
	    Dim userName As New SqlParameter("@UserName", SqlDbType.VarChar, 50)
	    userName.Value = txtUserName.Text.ToString()
	    cmd.Parameters.Add(userName)	    Dim pwd As New SqlParameter("@Password", SqlDbType.VarChar, 50)
	    pwd.Value = txtPassword.Text.ToString()
	    cmd.Parameters.Add(pwd)
	    Dim email As New SqlParameter("@Email", SqlDbType.VarChar, 50)
	    email.Value = txtEmail.Text.ToString()
	    cmd.Parameters.Add(email)
	    Dim status As New SqlParameter("@Status", SqlDbType.VarChar, 10)
	    status.Value = drpStatus.SelectedItem.ToString()
	    cmd.Parameters.Add(status)

	    Try		    con.Open()
		    cmd.ExecuteNonQuery()
		    lblMsg.Text = "User Registration successful"
	    Catch ex As SqlException		    Dim errorMessage As String = "Error in registering user"
		    errorMessage += ex.Message
		    Throw New Exception(errorMessage)	    Finally
		    con.Close()
	    End Try
    End Sub
End Class

The Design Layer:
<asp:Content ID="Content5" ContentPlaceHolderID="Register" Runat="Server">
    <div class="center_content">
	    <h2>Create a New Account</h2>
		   <p>Use the form below to create a new account.</p>
		   <p>Passwords are required to be a minimum of 6 characters in length.</p>
		    <span class="failureNotification">
				    <asp:Literal ID="ErrorMessage" runat="server"></asp:Literal>
		    </span>
				 	 <div class="accountInfo">
		  <fieldset class="register" style="width: 500px">
			  <legend>Account Information</legend>
			    <p>
			   
				   <asp:Label
				   ID="lblFirstName"
				   runat="server"
				   AssociatedControlID="txtFirstName">First Name:</asp:Label>
				   <asp:TextBox
				   ID="txtFirstName"
				   runat="server"
				   CssClass="textEntry">
				   </asp:TextBox>				   <asp:RequiredFieldValidator
				   ID="FirstNameRequired"
				   runat="server"
				   ControlToValidate="txtFirstName"
				   CssClass="failureNotification"
				   Text="First Name is required."
				   SetFocusonerror="True" />
				  
			   </p>
			   <p>
				  <asp:Label
				  ID="lblLastName"
				  runat="server"
				  AssociatedControlID="txtLastName">Last Name:</asp:Label>				  <asp:TextBox
				  ID="txtLastName"
				  runat="server"
				  CssClass="textEntry"></asp:TextBox>
				 
				  <asp:RequiredFieldValidator
				  ID="LastNameRequired"
				  runat="server"
				  ControlToValidate="txtLastName"
				  CssClass="failureNotification"
				  Text="Last Name is required."
				  SetFocusonerror="True" />
			    </p>

			    <p>
				    <asp:Label
				    ID="lblUserName"
				    runat="server"
				    AssociatedControlID="txtUserName">User Name:</asp:Label>
				   
				    <asp:TextBox
				    ID="txtUserName"
				    runat="server"
				    CssClass="textEntry"></asp:TextBox>
				   
				    <asp:RequiredFieldValidator
				    ID="UserNameRequired"
				    runat="server"
				    ControlToValidate="txtUserName"
				    CssClass="failureNotification"
				    Text="User Name is required."
				    SetFocusonerror="True" />
				   
			    </p>			    <p>
				    <asp:Label
				    ID="lblPwd"
				    runat="server"
				    AssociatedControlID="txtPassword">Password:</asp:Label>
				   
				    <asp:TextBox
				    ID="txtPassword"
				    runat="server"
				    CssClass="passwordEntry"
				    TextMode="Password" Width="128px"></asp:TextBox>
				   
				    <asp:RequiredFieldValidator
				    ID="PasswordRequired"
				    runat="server"
				    ControlToValidate="txtPassword"
				    CssClass="failureNotification"
				    Text="Password is required."
				    SetFocusonerror="True" />
				   
				  </p>
				  <p>
					 <asp:Label
					 ID="lblConfPwd"
					 runat="server"
					 AssociatedControlID="txtConfPwd">Confirm Password:</asp:Label>
							   
					 <asp:TextBox
					 ID="txtConfPwd"
					 runat="server"
					 CssClass="passwordEntry"
					 TextMode="Password" ></asp:TextBox>
					
					 <asp:RequiredFieldValidator
					 ControlToValidate="txtConfPwd"
					 CssClass="failureNotification"
					 Display="Dynamic"
					 Text="Confirm Password is required."
					 ID="ConfirmPasswordRequired"
					 runat="server"
					 SetFocusonerror="True"/>
					
					
					 <asp:CompareValidator
					 ID="PasswordCompare"
					 runat="server"
					 ControlToCompare="txtPassword"
					 ControlToValidate="txtConfPwd"
					 CssClass="failureNotification"
					 Display="Dynamic"
					 Text="The Password and Confirmation Password must match." />
					
			    </p>			    <p>
					 <asp:Label
					 ID="lblEmail"
					 runat="server"
					 AssociatedControlID="txtEmail">E-mail:</asp:Label>
					
					 <asp:TextBox
					 ID="txtEmail"
					 runat="server"
					 CssClass="textEntry"></asp:TextBox>
					
					 <asp:RequiredFieldValidator
					 ID="EmailRequired"
					 runat="server"
					 ControlToValidate="txtEmail"
					 CssClass="failureNotification"
					 Text="E-mail is required."
					 ToolTip="E-mail is required."
					 SetFocusonerror="True" />
					
					 <asp:RegularExp<b></b>ressionValidator
					 ID="RegularExp<b></b>ressionValidator1"
					 runat="server"
					 ValidationExp<b></b>ression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
					 ControlToValidate="txtEmail"
					 CssClass="failureNotification"
					 Text="Invalid Email Format. Re-Type Email!!"></asp:RegularExp<b></b>ressionValidator>
						   
			    </p>
			   
			    <p>
				    <asp:Label
					 ID="lblStatus"
					 runat="server"
					 AssociatedControlID="drpStatus">Status:</asp:Label>
				    <asp:DropDownList ID="drpStatus" runat="server">
				   
				    <asp:ListItem Selected="True">--Select Status--</asp:ListItem>
				    <asp:ListItem>Single</asp:ListItem>
				    <asp:ListItem>Married</asp:ListItem>
				   
				   
				    </asp:DropDownList>
					
			    </p>
									   
			    <p>
						    <asp:Button ID="btnSubmit"
							   runat="server"
							   CssClass="Notification"
							   Text="Register" />
			    </p>					    <p>
						    <asp:Label ID="lblMsg" runat="server">
							  </asp:Label>
					    </p>
		  </fieldset>
			   
	   </div>	   
    </div>
</asp:Content>


Now My Login Form .aspx:

<div class="center_content">
	    <h2 >
		    Log In
	    </h2>
    <p>
	    Please enter your username and password.
	    <asp:HyperLink
	    ID="RegisterHyperLink"
	    NavigateUrl="~/JJS/Register.aspx"
	    Text="Register"
	    runat="server"
	    EnableViewState="false">Register</asp:HyperLink> if you don't have an account.    </p>
   <div id="accountInfo">
    <fieldset id="login" style="width: 450px">
				    <legend>Account Information</legend>
				    <p>
					    <asp:Label
					    ID="UserNameLabel"
					    runat="server"
					    AssociatedControlID="UserName">Username:</asp:Label>					    <asp:TextBox
					    ID="UserName"
					    runat="server"
					    CssClass="textEntry"></asp:TextBox>
					    <asp:RequiredFieldValidator
					    ID="UserNameRequired"
					    runat="server"
					    ControlToValidate="UserName"
					    CssClass="failureNotification"
					    ErrorMessage="User Name is required."
					    ToolTip="User Name is required."
					    ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator>
				    </p>
				    <p>
					    <asp:Label
					    ID="PasswordLabel"
					    runat="server"
					    AssociatedControlID="Password">Password:</asp:Label>					    <asp:TextBox
					    ID="Password"
					    runat="server"
					    CssClass="passwordEntry"
					    TextMode="Password"></asp:TextBox>
					    <asp:RequiredFieldValidator
					    ID="PasswordRequired"
					    runat="server"
					    ControlToValidate="Password"
					    CssClass="failureNotification"
					    ErrorMessage="Password is required."
					    ToolTip="Password is required."
					    ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator>
				    </p>
				    <p>
					    <asp:CheckBox ID="RememberMe" runat="server"/>
					   
					    <asp:Label ID="RememberMeLabel" runat="server" AssociatedControlID="RememberMe" CssClass="inline">Keep me logged in</asp:Label>
				    </p>				    <p>
					    <asp:Button ID="btnLogin" runat="server" Text="Log In" />
				    </p>
				    <p>
					   
								  </p>
			    </fieldset>
    </div>
    </div>

The Code-Behind:

Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Partial Class JJS_Main
    Inherits System.Web.UI.Page
    Private ReadOnly _conString As String    Public Sub New()
	    _conString = WebConfigurationManager.ConnectionStrings("jjshopconn").ConnectionString
    End Sub
    Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogin.Click
	    Dim con As New SqlConnection(_conString)
	    Dim sqlstring As String = "SELECT * FROM tbl_Customer WHERE  (Cust_uname ='" & UserName.Text & "' AND Cust_pwd ='" & Password.Text & "')"	    Dim comm As New System.Data.SqlClient.SqlCommand(sqlstring, con)
	    Dim reader As System.Data.SqlClient.SqlDataReader
	    con.Open()	    reader = comm.ExecuteReader()

	    If reader.Read() Then
		    Response.Redirect("Default.aspx")
	    Else
		    lblStatus.Text = "Try Again! Please Re-Enter your Login and Password!"
	    End If	    con.Close()
    End Sub
End Class


If you could help me Please...
  • 0

#2 VNFox

VNFox

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 648 posts
  • Programming Language:C#, PHP
  • Learning:Assembly

Posted 24 October 2012 - 12:35 PM

What's the exception error ? It's nice that you have the code formatted ... but it would be nicer if you attach as zip file.
  • 0

www.pickmike.com
I don't just develop software. I find solutions to your business needs.


#3 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash

Posted 24 October 2012 - 01:45 PM

Have you run this in the debugger?

One thing you should also do is to capture the number of rows affected.

you have:

cmd.ExecuteNonQuery()
lblMsg.Text = "User Registration successful"

But you'd be better off with something like:

int numRowsAffected = cmd.ExecuteNonQuery()

if(numRowsAffected == 0)
throw new ..... <<= Whatever exception you want here.

lblMsg.Text = "User Registration successful"

But I would put a breakpoint on that row and verify that:

a. you're getting that far.
b. if any rows are coming up as inserted.

Just noticed your code was in VB.NET. My example was in C#, but it should be understandable.
  • 0
My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth

#4 luffy mcawesome

luffy mcawesome

    CC Newcomer

  • Member
  • PipPip
  • 12 posts

Posted 25 October 2012 - 12:35 AM

Oh My God Thanks!!!...

I'd like to do something about the login form:

like when i load the login page, the values are already in the textboxes (Something like 'Remember me')

An extract code how to do the remember me Please

Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogin.Click
	    Dim con As New SqlConnection(_conString)
	    Dim sqlstring1 As String = "SELECT * FROM tbl_Customer WHERE  (Cust_uname ='" & UserName.Text & "' AND Cust_pwd ='" & Password.Text & "')"
	    Dim cmd As New SqlCommand(sqlstring1, con)
	    Dim sdr As System.Data.SqlClient.SqlDataReader
	    Dim LoginCookies As New HttpCookie("LoginCookies")

	    Try
		    con.Open()
		    cmd = New SqlCommand(sqlstring1, con)
		    cmd.Parameters.AddWithValue("@username", UserName.Text)
		    cmd.Parameters.AddWithValue("@password", Password.Text)
		    sdr = cmd.ExecuteReader()
		    If sdr.Read() Then
			   
			    Session("UserName") = UserName.Text
			    Session.Timeout = 20
	 Response.Cookies("LoginCookies").Value = UserName.Text
	 Response.Cookies("LoginCookies").Expires = DateTime.Now.AddHours(1)
			   
			   
			    Response.Cookies.Add(LoginCookies)
			   
			    Response.Redirect("Default.aspx")
		    Else
			    lblStatus.Text = "Invalid Username OR Password"
		    End If
		    con.Close()
	    Catch ex As Exception
		    Response.Write(ex.Message + "<br/>" + ex.StackTrace)
	    Finally
		    con.Close()
	    End Try
    End Sub

  • 0

#5 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

Posted 29 October 2012 - 12:18 AM

Luffy,
What would you like to do about that code?

clear the 'remember me' maybe?
  • 0

#6 luffy mcawesome

luffy mcawesome

    CC Newcomer

  • Member
  • PipPip
  • 12 posts

Posted 29 October 2012 - 04:14 AM

exactly is: the user will enter his credentials to login, when he comes back to log In after he has closed the application, his username and password are already viewed in the texboxes

i don't know precisely how to achieve that by using cookies to bind it to the 'remember me' checkbox

that is if the checbox is ticked, the cookie will remember his info or if the chkBox is null the user will have to re-enter his login/username and Password


Thank You
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download