Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public partial class frmLogin : Form
- {
- public frmLogin()
- {
- InitializeComponent();
- }
- //Connection string
- string cs = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Pontus\Documents\Visual Studio 2017\Projects\Test\Login\Login\login.mdf;Integrated Security=True;Connect Timeout=30";
- private void btn_Submit_Click(object sender, EventArgs e)
- {
- if (txt_Username.Text=="" || txt_Password.Text=="")
- {
- MessageBox.Show("Please provide Username and Password.");
- return;
- }
- try
- {
- //Create SQLConnection
- SqlConnection con = new SqlConnection(cs);
- SqlCommand cmd = new SqlCommand("SELECT * FROM tbl_Login WHERE Username=@Username AND Password=@password", con);
- cmd.Parameters.AddWithValue("@Username", txt_Username.Text);
- cmd.Parameters.AddWithValue("@Password", txt_Password.Text);
- con.Open();
- SqlDataAdapter adapt = new SqlDataAdapter(cmd);
- DataSet ds = new DataSet();
- adapt.Fill(ds);
- con.Close();
- int count = ds.Tables[0].Rows.Count;
- //If count is equal to 1, then show frmmain Form
- if (count == 1)
- {
- MessageBox.Show("Login Successful!");
- this.Hide();
- frmAdmin fa = new frmAdmin();
- fa.Show();
- }
- else
- {
- MessageBox.Show("Login Failed!");
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- private void frmLogin_Load(object sender, EventArgs e)
- {
- }
- private void checkBox_Show_Password_CheckedChanged(object sender, EventArgs e)
- {
- txt_Password.PasswordChar = checkBox_Show_Password.Checked ? '\0' : '*';
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement