0

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.

3
Contributors
7
Replies
12
Views
6 Years
Discussion Span
Last Post by dimastea
Featured Replies
  • 1
    __avd 1,826   6 Years Ago

    Have a look, [code] 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; } [/code] Read More

  • 1
    __avd 1,826   6 Years Ago

    [code] Form2 frm=new Form2(); frm.Show(); this.Close(); [/code] Read More

1

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;
        }
0

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 by dawsonz: n/a

0
private void btnSubmit_Click(object sender, EventArgs e)
        {

            UserID = new UserLogin();

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

        }
0

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.