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.

Have a look,

public Boolean Login(String username, String password)
        {
            SqlCommand cmd = new SqlCommand ("SELECT * FROM [users]  WHERE [username]='" + username + "' AND [password]='" + password + "'", connection);
            connection.Open();
            SqlDataReader reader = cmd.ExecuteReader();

            bool found=false;
            if(reader.Read())
            {
                found=true;
            }
             reader.Close();
             connection.Close();
            return found;
        }

Ok thanks i've just tried that now.

How can I get it to close my current Form and open Form2 if the information is correct?

Edited 6 Years Ago by dawsonz: n/a

private void btnSubmit_Click(object sender, EventArgs e)
        {

            UserID = new UserLogin();

            if(UserID.Login(txtUsername.Text, txtPassword.Text))
                {
                        //Login success
                }
             else
                {
                      // Fails
                 }

        }

Thank you ever so much that's working perfectly.

When I did VB i simply used:
Form1.close();
Form2.show();

However this doesn't work, whats the alternative in c# please?

This question has already been answered. Start a new discussion instead.