Hi,
I'm trying to create a Web Form in C# - the form has a username and password textbox. The user enters there username and password which I want the program to check if it exists in the SQL database.
I'm trying to create a login.cs class and then run the code on my form to check if the user information is correct however i'm struggling.
My login.cs class:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace assignmentForm
{
public class UserLogin
{
private SqlConnection connection = new SqlConnection();
public UserLogin()
{
///create sql connection
connection.ConnectionString = @"Data Source=localhost\sqlexpress;Initial Catalog=coffee;Integrated Security=True;Pooling=False";
connection.Open();
}
public Boolean Login(String username, String password)
{
SqlCommand cmd = new SqlCommand ("SELECT * FROM users WHERE username='" + username + "' AND password ='" + password + "'", connection);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
String s = (String)reader[0];
}
return false;
}
}
My Web Form (Form1):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Text;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Configuration;
namespace assignmentForm
{
public partial class Form1 : Form
{
private UserLogin UserID;
public Form1()
{
InitializeComponent();
}
private void btnSubmit_Click(object sender, EventArgs e)
{
UserID = new UserLogin();
UserID.Login(txtUsername.Text, txtPassword.Text);
}
}
}
Now I just want a simple check on my database to see if that username and password matches my SQL database.
Can anyone provide any help i'd be very grateful.