Forums - C# Corner

Forum guidelines
Michael Taylor

Michael Taylor

  • 1.8k
  • 4
  • 88

Making a Login Form Connected to a DB

May 21 2019 10:45 AM
I've been trying to teach myself how to do this for a few days now but can't figure out certain things.
 
This is what I have currently and I can't, for the life of me, figure out where I'm wrong at. I hope this isn't a lost cause as I'm trying to help my school get out of using paper for certain things and this is my first step.
  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 System.Data.Sql;  
  11. using System.Data.SqlClient;  
  12. namespace CCCLogin  
  13. {  
  14. public partial class Form2 : Form  
  15. {  
  16. public Form2()  
  17. {  
  18. InitializeComponent();  
  19. }  
  20. private void Exit_Click(object sender, EventArgs e)  
  21. {  
  22. this.Close();  
  23. }  
  24. private void Login_Click(object sender, EventArgs e)  
  25. {  
  26. SqlConnection con = new SqlConnection(@"Data Source=(LOCALDB)\MSSQLLocalDB.C:\USERS\MICHAELC0837\DOCUMENTS\CCCLOGIN\CCCEMPLOYEES.mdf; Initial Catalog=CCCEMPLOYEES.mdf; Integrated Security=True");  
  27. SqlDataAdapter sda = new SqlDataAdapter("select count(*) from Username where username='" + UserTxtBox + "and password='" + PassTxtBox + "'", con);  
  28. DataTable dt = new DataTable();  
  29. sda.Fill(dt);  
  30. if (dt.Rows[0][0]).ToString() == "1")  
  31. {  
  32. this.Hide();  
  33. CCCLogin ss = new CCCLogin();  
  34. ss.Show();  
  35. }  
  36. else  
  37. MessageBox.Show("please enter correct username and password""alert", MessageBoxButtons.OK, MessageBoxIcon.Error);  
  38. }  
  39. }  
  40. }  
  41. } 

Answers (3)