Hemal Joshi

Hemal Joshi

  • NA
  • 78
  • 47.2k

Role Based User Login with Switch Case in Windows App in C#

Jan 3 2018 5:20 AM
Hello,
 
I have created a Role Based Login form in Windows Form Application in C# there i have used switch case for selecting role. 
 
When i am run the form and enter Username and Password worng it will not go to the default block of the switch case so help me and give me an example of role based login form in windows application.
 
Design: 
 
 
 
Code:
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace DemoLogin
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\admin\Documents\DemoCon.mdf;Integrated Security=True;Connect Timeout=30");
SqlDataAdapter da = new SqlDataAdapter("select Type from Login where Username='" + textBox1.Text +"' and Password='" + textBox2.Text + "'",con);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count == 1)
{
switch (dt.Rows[0]["Type"] as string)
{
case "Admin":
{
this.Hide();
new HomeMenus().Show();
break;
}
case "User":
{
MessageBox.Show("You are User and only view the things");
break;
}
case "Other":
{
new Others().Show();
break;
}
default:
{
MessageBox.Show("Enter Correct Username and Password");
break;
}
}
textBox1.Text = "";
textBox2.Text = "";
}
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
 
Database: 
 

Answers (8)