Advertisement
Guest User

Untitled

a guest
Nov 13th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.81 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using MySql.Data.MySqlClient;
  11.  
  12.  
  13. namespace FloristPOS
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         MySqlConnection con = new MySqlConnection(@"Server=localhost;Database=florist;Uid=root;Pwd=1234;");
  18.         int i;
  19.         public Form1()
  20.         {
  21.             InitializeComponent();
  22.         }
  23.  
  24.         private void button1_Click(object sender, EventArgs e)
  25.         {
  26.             i = 0;
  27.             //Open Database Connection
  28.             con.Open();
  29.             //Create mysql command
  30.             MySqlCommand cmd = con.CreateCommand();
  31.             cmd.CommandType = CommandType.Text;
  32.             //Query to select all the username and password from the database which corresponds to textboxes
  33.             cmd.CommandText = "select * from users where Username='" + txtUser.Text + "' and Password='" + txtPass.Text + "'";
  34.             //Execute the query
  35.             cmd.ExecuteNonQuery();
  36.             DataTable dt = new DataTable();
  37.             MySqlDataAdapter da = new MySqlDataAdapter(cmd);
  38.  
  39.             da.Fill(dt);
  40.             i = Convert.ToInt32(dt.Rows.Count.ToString());
  41.  
  42.             //Messagebox if incorrect credentials
  43.  
  44.             if (i == 0)
  45.             {
  46.                 MessageBox.Show("Invalid username or password", "Login Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
  47.                 txtPass.Text = "";
  48.                 txtUser.Text = "";
  49.  
  50.             }
  51.             else
  52.             {
  53.  
  54.  
  55.                 this.Hide();
  56.                 Form2 fm = new Form2();
  57.                 fm.Show();
  58.  
  59.  
  60.             }
  61.             con.Close();
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement