0

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.

Attachments
<%@ 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
4
Contributors
7
Replies
8
Views
7 Years
Discussion Span
Last Post by aurora1234
0

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.

0

Hi! I receive no errors, just that the system kicks me back to the login page with "username" field filled.

0

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.

0

you r passing arguments while calling Function DbConnection()
but you not specified any argument while defining it

0

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
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.