Click here to Skip to main content
11,431,483 members (63,970 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET VB.NET
I want to make login for 2 types: Admin & Regualar users, each one had diff table, where I`ll check the type by dropdownlist,

problem: it wont get into if statement that check the type. how to make it check whats the value of the droplist and depends on that it`ll decide to check in which table.



Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Data
Imports System.Threading
 

Public Class _Default
Inherits System.Web.UI.Page
Public s As String
 
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Session.Clear()
 
End Sub
 
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    Dim con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("RegconnectionString").ConnectionString)
    con.Open()
    Dim userCount As Int32 = GetUserCount(txtUserName.Text)
    Dim paasCount As Int32 = GetPassCount(TxtPassword.Text)
    Dim userCount2 As Int32 = GetUserCount(txtUserName.Text)
    Dim paasCount2 As Int32 = GetPassCount(TxtPassword.Text)
    Dim x As String = DrpType.SelectedValue
 
    If x = "Admin" Then
        If userCount2 > 0 And paasCount2 > 0 Then
            Session("FirstName") = txtUserName.Text
            Response.Redirect("LoggedIn.aspx")
            ClientScript.RegisterStartupScript(Page.GetType(), "Message", "alert('Admin')", True)
        Else
            lblErr.Visible = True
        End If
 

    ElseIf x = "Cus" Then
        If userCount > 0 And paasCount > 0 Then
            Session("FirstName") = txtUserName.Text
            Response.Redirect("LoggedIn.aspx")
            ClientScript.RegisterStartupScript(Page.GetType(), "Message", "alert('Customer')", True)
        Else
            lblErr.Visible = True
        End If
    End If
End Sub
 

' Method to check existence for users
Public Shared Function GetUserCount(ByVal userName As String) As Int32
    Const sql1 = "SELECT COUNT(*) FROM Registration where username = @UserName"
    Using con As New SqlConnection(ConfigurationManager.ConnectionStrings("RegconnectionString").ConnectionString)
        Using cmd = New SqlCommand(sql1, con)
            cmd.Parameters.AddWithValue("@UserName", userName)
            con.Open()
            Using reader = cmd.ExecuteReader()
                If reader.HasRows Then
                    reader.Read()
                    Dim count As Int32 = reader.GetInt32(0)
                    Return count
                End If
            End Using
        End Using
    End Using
End Function
 

' Method to check PAssword for users
Public Shared Function GetPassCount(ByVal password As String) As Int32
    Const sql2 = "SELECT COUNT(*) FROM Registration where password = @Password"
    Using con As New SqlConnection(ConfigurationManager.ConnectionStrings("RegconnectionString").ConnectionString)
        Using cmd = New SqlCommand(sql2, con)
            cmd.Parameters.AddWithValue("@Password", password)
            con.Open()
            Using reader = cmd.ExecuteReader()
                If reader.HasRows Then
                    reader.Read()
                    Dim count As Int32 = reader.GetInt32(0)
                    Return count
                End If
            End Using
        End Using
    End Using
End Function
 

        ' Method to check existence for admin
Public Shared Function GetUserCount2(ByVal userName2 As String) As Int32
    Const sql3 = "SELECT COUNT(*) FROM Adm_Login where username = @UserName"
    Using con As New SqlConnection(ConfigurationManager.ConnectionStrings("RegconnectionString").ConnectionString)
        Using cmd = New SqlCommand(sql3, con)
            cmd.Parameters.AddWithValue("@UserName", userName2)
            con.Open()
            Using reader = cmd.ExecuteReader()
                If reader.HasRows Then
                    reader.Read()
                    Dim count As Int32 = reader.GetInt32(0)
                    Return count
                End If
            End Using
        End Using
    End Using
End Function
 

         ' Method to check PAssword for admin
Public Shared Function GetPassCount2(ByVal password2 As String) As Int32
    Const sql4 = "SELECT COUNT(*) FROM Adm_Login where password = @Password"
    Using con As New SqlConnection(ConfigurationManager.ConnectionStrings("RegconnectionString").ConnectionString)
        Using cmd = New SqlCommand(sql4, con)
            cmd.Parameters.AddWithValue("@Password", password2)
            con.Open()
            Using reader = cmd.ExecuteReader()
                If reader.HasRows Then
                    reader.Read()
                    Dim count As Int32 = reader.GetInt32(0)
                    Return count
                End If
            End Using
        End Using
    End Using
End Function
 

Public Sub DrpType_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DrpType.SelectedIndexChanged
 
End Sub
 
End Class
Posted 23-Dec-12 10:26am
conanzz307
Comments
Sampath Kumar Sathiya at 23-Dec-12 23:18pm
   
Have you made the autopostback true for the dropdownlist?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Even I am a bit new to .NET

Anyways, use

If x == "Admin" Then


and put a break point to check the flow.


Hope it helps.

Cheers.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

There are too many flaws in your code. First of all how do you bind your dropdown list? Have you hard coded it(you have included listitems in aspx page)? Why do you need a postback when dropdownlist's selected index is changed when you are validating user's credentials on button's click? You are validating user or admin in two parts (user id and password seperately)! Suppose there are two users user1 and userr2 user1 has password password1 and user2 has password password2, now when user1 logs in he enters username: user1 and password: password2. This is incorrect information, still your code will authenticate the user and allows him to log in. And same goes for Admin. In order to make it work, follow these steps:

1) Ask the user to enter its user id and password and select the type from dropdownlist(user or admin)
2) Apply proper server-side and client-side validation before checking the information against database.
3) write two functions to validate users validateUser(string _userName, string _password) and validateAdmin(string _userName, string _password)
4) On button's click event write:

if(DrpType.SelectedItem.Text == "Admin")
validateAdmin(txtUserName.Text,TxtPassword.Text)
else
validateUsertxtUserName.Text,TxtPassword.Text)
Both the functions should validate the user based on username "and" password.

Hope it helps.
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web01 | 2.8.150428.2 | Last Updated 24 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100