I created C#.net Windows Application for Control User Login, But I cant Implement that Asp.net Application. This is my code.
//In Form 1........
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace UserPermission
{
public partial class Form1 : Form
{
private readonly string conn_str = "Data Source=nuwan-pc;Initial Catalog=UserPermission;Integrated Security=True";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnLogin_Click(object sender, EventArgs e)
{
string query = "SELECT Id, Username, Password, PriviledgeLvl FROM Access WHERE Username = @Username";
using (SqlConnection conn = new SqlConnection(conn_str))
{
using (SqlCommand cmd = new SqlCommand(query, conn))
{
//add a parameter to sql query
cmd.Parameters.AddWithValue("Username", txtUsername.Text.Trim());
conn.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
reader.Read();
string password = reader["Password"].ToString();
string priviledgeLevel = reader["PriviledgeLvl"].ToString();
string id = reader["Id"].ToString();
if (reader.Read())
{
return;
}
else
{
if (txtPassword.Text.Trim() == password)
{
//Create new form2 and pass it the 'priviledgeLevel' parameter
Form2 frm2 = new Form2(priviledgeLevel,id,m);
frm2.ShowDialog();
}
else
{
MessageBox.Show("Password or Username Not Valid"); //Wrong password!
}
}
}
}
}
}
}
}
}
//In Form 2.........
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace UserPermission
{
public partial class Form2 : Form
{
public Form2(string priviledgeLevel)
{
InitializeComponent();
InitControls(priviledgeLevel);
}
private void InitControls(string priviledgeLevel)
{
if (priviledgeLevel == "ADMIN")
{
//buttons visible to admin only
btnFirst.Visible = true;
btnSecond.Visible = true;
btnThird.Visible = true;
}
else
{
//buttons not visible to common user
btnFirst.Visible = false;
btnSecond.Visible = false;
btnThird.Visible = false;
}
//These two buttons are visible to both admin and common user
btnFourth.Visible = true;
btnFifth.Visible = true;
}
}
}
Please Help me that matter.........
Rathnayake