Reputation: 19
So, I'm working on a project for a class of mine. The first part is a login form, which requires a user to enter a username and a password. When the login button is hit, the program is to compare the textbox text to what is in a datatable. Only problem is, I'm having a tough time doing this. I tried doing it with LINQ statements, but that made the values different from what I was expecting when I went to debug it. Am I doing something wrong here? Heres the code for the form.
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;
using System.Data.Entity;
using System.Data.Entity.Validation;
namespace mcshonsey_Final
{
public partial class LoginForm : Form
{
SortingClass sort = new SortingClass();
mcshonsey_FinalProject.UserShowDBEntities dbcontext = null;
public LoginForm()
{
InitializeComponent();
textBox1.Text = "";
textBox2.Text = "";
textBox1.Focus();
}
private void button1_Click(object sender, EventArgs e)
{
if (dbcontext != null)
dbcontext.Dispose();
dbcontext = new mcshonsey_FinalProject.UserShowDBEntities();
dbcontext.UserTables
.OrderBy(entry => entry.UserID)
.Load();
if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))
{
MessageBox.Show("You must enter a password or username", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox1.Focus();
}
/*else
{
ShowSelectForm ssf = new ShowSelectForm(this, sort);
Hide();
ssf.Show();
}*/
string num1 = Convert.ToString(textBox1.Text);
string num2 = Convert.ToString(textBox2.Text);
var user =
from use in dbcontext.UserTables
where use.UserName == num1
select use;
var user2 =
from pas in dbcontext.UserTables
where pas.UserPassword == num2
select pas;
if (textBox1.Text.Equals(user) && textBox2.Text.Equals(user2))
{
ShowSelectForm ssf = new ShowSelectForm(this, sort);
Hide();
ssf.Show();
}
else
{
MessageBox.Show("Incorrect username and/or password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox1.Focus();
}
}
private void button2_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Are you sure you want to quit?", "Exit", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
Application.Exit();
}
}
private void LoginForm_Load(object sender, EventArgs e)
{
}
}
}
The SortingClass is a class to sort through the datatable, but that's for a later time. The UserShowDBEntities is the database itself.
Upvotes: 0
Views: 72
Reputation: 97
I hope your data source is filled and your password is not encrypted in the database.
var user = dbcontext.UserTables.FirstOrDefault( u => u.UserName == textBox1.Text && u.UserPassword == textBox2.Text);
if(user != null)
// Check
else
// Failed
If textBox1
is the username and textBox2
is the password, this should work.
Upvotes: 0
Reputation: 288
I'm not a user of LINQ to SQL but I believe the following would work for you.
Basically, I made the following changes: 1. Combined the username and password check into a single WHERE clause 2. If you get a matching record back (i.e. Enumerable.Count check) it means the username and password matched a record and thus were correct.
private void button1_Click(object sender, EventArgs e)
{
if (dbcontext != null)
dbcontext.Dispose();
dbcontext = new mcshonsey_FinalProject.UserShowDBEntities();
dbcontext.UserTables
.OrderBy(entry => entry.UserID)
.Load();
if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))
{
MessageBox.Show("You must enter a password or username", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox1.Focus();
}
/*else
{
ShowSelectForm ssf = new ShowSelectForm(this, sort);
Hide();
ssf.Show();
}*/
var user =
from use in dbcontext.UserTables
where use.UserName == textBox1.Text && use.Password == textBox2.Text
select use;
if (Enumerable.Count(user) > 0)
{
ShowSelectForm ssf = new ShowSelectForm(this, sort);
Hide();
ssf.Show();
}
else
{
MessageBox.Show("Incorrect username and/or password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox1.Focus();
}
}
Upvotes: 1