Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Login
Sign Up
Ask Question
8
Reply
Role Based User Login with Switch Case in Windows App in C#
Hemal Joshi
Jan 3 2018 5:20 AM
171
Reply
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:
Upload Source Code
Select only zip and rar file.
Post
Reset
Cancel
Answers (
8
)
Next Recommended Forum
".dll is not valid test extension" - when automate coded ui
How to read emails from *.pst file in c#