Hey BubunDash,
Please refer below sample.
HTML
C#
<asp:Button ID="Button1" Text="UserLogin" runat="server" OnClick="UserLogin" /><br />
<br />
<asp:Button ID="Button2" Text="AdminLogin" runat="server" OnClick="AdminLogin" /><br />
<asp:Panel runat="server" ID="PanelUser">
<asp:Label Text="UserName" runat="server" ID="lblUserName" />
<asp:TextBox runat="server" ID="Text_UserName" />
<asp:Label ID="lblUserPass" Text="UserPassword" runat="server" />
<asp:TextBox runat="server" ID="Text_Password" />
</asp:Panel>
<asp:Panel runat="server" ID="PanelAdmin">
<asp:Label ID="lblAdminName" Text="AdminName" runat="server" />
<asp:TextBox runat="server" ID="Text_AdminUserName" />
<asp:Label ID="lblAdminPassword" Text="AdminPassword" runat="server" />
<asp:TextBox runat="server" ID="Text_AdminPassword" />
</asp:Panel>
Namespaces
C#
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
PanelUser.Visible = false;
PanelAdmin.Visible = false;
}
}
protected void UserLogin(object sender, EventArgs e)
{
string name = Text_UserName.Text;
string password = Text_Password.Text;
if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(password))
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT Name, Country FROM Customers WHERE Name =@Name AND Country = @Country", con))
{
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@Country", password);
con.Open();
string value = Convert.ToString(cmd.ExecuteScalar());
if (!string.IsNullOrEmpty(value))
{
//User Panel
Response.Redirect("Default.aspx");
}
else
{
Text_UserName.Focus();
Text_UserName.BackColor = System.Drawing.Color.Yellow;
}
con.Close();
}
}
}
else
{
PanelUser.Visible = true;
}
}
protected void AdminLogin(object sender, EventArgs e)
{
string name = Text_AdminUserName.Text;
string password = Text_AdminPassword.Text;
if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(password))
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT Name, Country FROM Customers WHERE Name =@Name AND Country = @Country", con))
{
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@Country", password);
con.Open();
string value = Convert.ToString(cmd.ExecuteScalar());
if (!string.IsNullOrEmpty(value))
{
//Admin Panel
Response.Redirect("Admin.aspx");
}
else
{
Text_AdminUserName.Focus();
Text_AdminUserName.BackColor = System.Drawing.Color.Yellow;
}
con.Close();
}
}
}
else
{
PanelAdmin.Visible = true;
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
PanelUser.Visible = False
PanelAdmin.Visible = False
End If
End Sub
Protected Sub UserLogin(ByVal sender As Object, ByVal e As EventArgs)
Dim name As String = Text_UserName.Text
Dim password As String = Text_Password.Text
If Not String.IsNullOrEmpty(name) AndAlso Not String.IsNullOrEmpty(password) Then
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT Name, Country FROM Customers WHERE Name =@Name AND Country = @Country", con)
cmd.Parameters.AddWithValue("@Name", name)
cmd.Parameters.AddWithValue("@Country", password)
con.Open()
Dim value As String = Convert.ToString(cmd.ExecuteScalar())
If Not String.IsNullOrEmpty(value) Then
Response.Redirect("Default.aspx")
Else
Text_UserName.Focus()
Text_UserName.BackColor = System.Drawing.Color.Yellow
End If
con.Close()
End Using
End Using
Else
PanelUser.Visible = True
End If
End Sub
Protected Sub AdminLogin(ByVal sender As Object, ByVal e As EventArgs)
Dim name As String = Text_AdminUserName.Text
Dim password As String = Text_AdminPassword.Text
If Not String.IsNullOrEmpty(name) AndAlso Not String.IsNullOrEmpty(password) Then
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT Name, Country FROM Customers WHERE Name =@Name AND Country = @Country", con)
cmd.Parameters.AddWithValue("@Name", name)
cmd.Parameters.AddWithValue("@Country", password)
con.Open()
Dim value As String = Convert.ToString(cmd.ExecuteScalar())
If Not String.IsNullOrEmpty(value) Then
Response.Redirect("Admin.aspx")
Else
Text_AdminUserName.Focus()
Text_AdminUserName.BackColor = System.Drawing.Color.Yellow
End If
con.Close()
End Using
End Using
Else
PanelAdmin.Visible = True
End If
End Sub
Screenshot
