I am following the code in a previous thread and cannot access SQL database. See attached login.aspx and login.aspx.vb files. Database file is a simple "Users" filw with AutoID, username, email and password. What am I doing wrong? Any help is greatly appreciated.
aurora1234 0 Newbie Poster
<%@ Page Title="Log In" Language="vb" MasterPageFile="~/Site.Master" AutoEventWireup="false"
CodeBehind="Login.aspx.vb" Inherits="iPesha.Login" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<link href="../Styles/Site.css" rel="stylesheet" type="text/css" />
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
Log In
</h2>
<p>
Please enter your username and password.
<asp:HyperLink ID="RegisterHyperLink" runat="server" EnableViewState="false">Register</asp:HyperLink> if you don't have an account.
</p>
<form name="form1" id="form1" method="post" action="Login.aspx">
<asp:Login ID="LoginUser" runat="server" EnableViewState="false" RenderOuterTable="false">
<LayoutTemplate>
<span class="failureNotification">
<asp:Literal ID="FailureText" runat="server"></asp:Literal>
</span>
<asp:ValidationSummary ID="LoginUserValidationSummary" runat="server" CssClass="failureNotification"
ValidationGroup="LoginUserValidationGroup"/>
<div class="accountInfo">
<fieldset class="login">
<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>
</fieldset>
<p class="submitButton">
<asp:Button ID="Login" runat="server" Text="Log In"
ValidationGroup="LoginUserValidationGroup" />
</p>
</div>
</LayoutTemplate>
</asp:Login>
</form>
</asp:Content>
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration
Imports System.Data.Common
Partial Class Login
Inherits System.Web.UI.Page
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Login_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoginUser.Click
If Page.IsValid Then
If DbConnection(LoginUser.UserName, LoginUser.Password) Then
'FormsAuthentication.RedirectFromLoginPage(UserName.Text, False)
Response.Redirect("JobSearch.aspx")
Else
Response.Redirect("JobSearch.aspx")
End If
End If
End Sub
Function DbConnection()
Dim username As String
Dim pwd As String
Dim pName As String
username = LoginUser.UserName
pwd = LoginUser.Password
pName = ""
Dim strConn As String
strConn = WebConfigurationManager.ConnectionStrings("iPesha").ConnectionString
Dim Conn As New SqlConnection(strConn)
Conn.Open()
Dim sqlUserName As String
sqlUserName = "SELECT UserName,Password FROM Users "
sqlUserName &= " WHERE (UserName = @UserName"
sqlUserName &= " AND Password = @Password)"
Dim com As New SqlCommand(sqlUserName, Conn)
com.Parameters.AddWithValue("@UserName", username)
com.Parameters.AddWithValue("@Password", pwd)
Dim CurrentName As String
CurrentName = CStr(com.ExecuteScalar)
If CurrentName <> "" Then
Session("UserAuthentication") = username
Response.Redirect("JobSearch.aspx")
Else
Session("UserAuthentication") = ""
End If
End Function
End Class
Lusiphur 185 Practically a Posting Shark Team Colleague Featured Poster
I guess to start with...
What, if any, errors are you receiving when you attempt to run this code? This might help to narrow down the specific components of the code to look at.
aurora1234 0 Newbie Poster
Hi! I receive no errors, just that the system kicks me back to the login page with "username" field filled.
Lusiphur 185 Practically a Posting Shark Team Colleague Featured Poster
I'm sorry, I did my best to wade through your code but I'm not much on VB. Hopefully someone with more in-depth knowledge of VB and SessionStates will be able to toss back an answer on this one for you.
aurora1234 0 Newbie Poster
Ok thanks anyway.
hirenpatel53 -1 Posting Whiz in Training
you r passing arguments while calling Function DbConnection()
but you not specified any argument while defining it
pritesh2010 30 Posting Whiz in Training
rty this code for login it properly works for me as i updated for you.
Dim strConn As String
strConn = WebConfigurationManager.ConnectionStrings("iPesha").ConnectionString
Dim Conn As New SqlConnection(strConn)
Conn.Open()
Dim sqlUserName As String= "SELECT UserName,Password FROM Users "
cmd =new sqlCommand(sqlUserName,con)
dim dr as SqldataReader
dr=cmd.ExecuteReader()
if dr.HasRows=true then
While(dr.Read())
If dr(1).ToString() = username And dr(2).ToString() = pwd Then
Session("UserAuthentication") = username
end if
end while
Response.Redirect("JobSearch.aspx")
elseif dr.HasRows=False then
Session("UserAuthentication") = ""
end if
aurora1234 0 Newbie Poster
That solved the problem. Thank you very much.
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.