Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Data;
- using System.Windows.Forms;
- using System.Data.SqlClient;
- using System.Diagnostics;
- namespace LoginApp
- {
- public partial class frmLogin : Form
- {
- public frmLogin()
- {
- InitializeComponent();
- }
- private void btnLogin_Click(object sender, EventArgs e)
- {
- SqlConnection sqlcon = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Tom\source\repos\LoginApp\DB\LoginDB.mdf;Integrated Security=True;Connect Timeout=30");
- // Talk about issue with 'Table' being keyword for T-SQL so having to put square brackets around it
- SqlCommand command = new SqlCommand(null, sqlcon);
- command.CommandText = "SELECT * FROM [Table] WHERE username = @username AND password = @password";
- SqlParameter usernameParam = new SqlParameter("@username", SqlDbType.VarChar, 50);
- SqlParameter passwordParam = new SqlParameter("@password", SqlDbType.VarChar, 50);
- usernameParam.Value = txtUsername.Text.Trim();
- passwordParam.Value = txtPassword.Text.Trim();
- command.Parameters.Add(usernameParam);
- command.Parameters.Add(passwordParam);
- command.Prepare();
- command.ExecuteNonQuery();
- SqlDataAdapter sda = new SqlDataAdapter(command);
- DataTable dtbl = new DataTable();
- sda.Fill(dtbl);
- if(dtbl.Rows.Count ==1)
- {
- var userId = dtbl.Rows[0]["userId"];
- LoggedInUserInformation.UserID = Convert.ToInt32(userId);
- frmMain objFrmMain = new frmMain();
- this.Hide();
- objFrmMain.Show();
- }
- else
- {
- MessageBox.Show("Check your username and password");
- }
- }
- private void frmLogin_Load(object sender, EventArgs e)
- {
- }
- private void btnExit_Click(object sender, EventArgs e)
- {
- this.Close();
- }
- private void btnCreateAccount_Click(object sender, EventArgs e)
- {
- UserRegistration objUserRegistration = new UserRegistration();
- this.Hide();
- objUserRegistration.Show();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement