0

Okay.. Currently i already had this code for my registration form

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;

namespace Thesis
{
    public partial class Registration : Form
    {
        Form formParent = null;
        public Registration(Form par)
        {
            formParent = par;
            InitializeComponent();
        }

        private void Registration_FormClosed(object sender, FormClosedEventArgs e)
        {
            formParent.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string connection = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Granodiorit\Documents\Visual Studio 2008\Projects\Thesis\Thesis\Database1.mdf;Integrated Security=True;User Instance=True";
            SqlConnection cn = new SqlConnection(connection);
            try
            {
                cn.Open();
            }
            catch (Exception)
            {
                MessageBox.Show("Did not connect");
            }

            string username = UserName.Text;
            string password = Password.Text;
            string nickname = Nick.Text;
            string birthday = Birthday.Text;
            string ymail = Ymail.Text;
            string gmail = Gmail.Text;
            string hotmail = Hotmail.Text;
            string yaddress = YAddress.Text;
            string gaddress = GAddress.Text;
            string hotaddress = HotAddress.Text;

            /* -------------------- */

            SqlCommand command = new SqlCommand("select username from [user] where username=@Username", cn);
            command.Parameters.AddWithValue("@UserName", username);

            object result = command.ExecuteScalar();
            if (result != null) // found
            {
                MessageBox.Show("Username already used");
                return;
            }

            /* -------------------- */

            string sqlquery;

            sqlquery = "INSERT INTO [User] (Username, Password, Nick, Birthday, Ymail, Yaddress, Gmail, Gaddress, Hotmail, Hotaddress) VALUES (@UserName, @Password, @Nick, @Birthday, @YMail, @YAddress, @GMail, @GAddress, @Hotmail, @HotAddress)";
            command = new SqlCommand(sqlquery, cn);
            command.Parameters.AddWithValue("@UserName", username);
            command.Parameters.AddWithValue("@Password", password);
            command.Parameters.AddWithValue("@Nick", nickname);
            command.Parameters.AddWithValue("@Birthday", birthday);
            command.Parameters.AddWithValue("@YMail", ymail);
            command.Parameters.AddWithValue("@YAddress", yaddress);
            command.Parameters.AddWithValue("@GMail", gmail);
            command.Parameters.AddWithValue("@GAddress", gaddress);
            command.Parameters.AddWithValue("@Hotmail", hotmail);
            command.Parameters.AddWithValue("@HotAddress", hotaddress);
            command.ExecuteNonQuery();
            this.Close();
            formParent.Show();
        }



        internal void show()
        {
            throw new NotImplementedException();
        }

        private void Registration_Load(object sender, EventArgs e)
        {
            string connection = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Granodiorit\Documents\Visual Studio 2008\Projects\Thesis\Thesis\Database1.mdf;Integrated Security=True;User Instance=True";
            SqlConnection cn = new SqlConnection(connection);
            try
            {
                cn.Open();
            }
            catch (Exception)
            {
                MessageBox.Show("Did not connect");
            }
        }

    }
}

and here is my Login form code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;


namespace Thesis
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Hide();
            Registration Form2 = new Registration(this);
            Form2.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string connection = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Granodiorit\Documents\Visual Studio 2008\Projects\Thesis\Thesis\Database1.mdf;Integrated Security=True;User Instance=True";
            SqlConnection cn = new SqlConnection(connection);
            try
            {
                cn.Open();
            }
            catch (Exception)
            {
                MessageBox.Show("Did not connect");
            }

            SqlCommand cmd = new SqlCommand ("SELECT * FROM [User]",cn);
            cmd.Connection = cn;
            SqlDataReader reader = null;
            reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                if(LoginUserName.Text == (reader["Username"].ToString()) && LoginPassword.Text == (reader["Password"].ToString()))
                {
                    MessageBox.Show("Login Succeed");
                }
            }
        }

    }
}

I dont know.. But somehow, the message box to let me know that the login form is succeed is not pop out.. anyone can help with my problem?

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by muzikhera
0

Try to use this line of code.
Just simply change your own variable then run.
May this line of code can help you.

private void btnLogin_Click(object sender, EventArgs e)
        {
            SqlConnection Connect = new SqlConnection();
            Connect.ConnectionString = @"Data Source=BURBANK\SQLEXPRESS;" + "Initial Catalog= ERPdb;" + "Persist Security Info=True;" + "User ID=antonette;" + "Password=ilovemis";
            Connect.Open();
            SqlCommand cmd = new SqlCommand("SELECT ISNULL(username, '') AS username, ISNULL(password, '') AS password FROM employee WHERE username='" + txtUserName.Text + "' and password='" + txtPassword.Text + "'", Connect);

            SqlDataReader dr = cmd.ExecuteReader();

            string userText = txtUserName.Text;
            string passText = txtPassword.Text;

            if (dr.Read())
            {
                frmMain main = new frmMain();
                this.Hide();
                main.Show();
            }
            else
                MessageBox.Show("Invalid UserName or Password.");

            {
                dr.Close();
                Connect.Close();
            }

Edited by mike_2000_17: Fixed formatting

This topic has been dead for over six months. 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.