Kevin Cope

Kevin Cope

  • NA
  • 8
  • 23.1k

Adding/reading Information from Database (Login/Register) problems

May 23 2011 5:53 PM
If i'm somehow in the wrong forum please say so.

For the past day I have been trying to create a register/login system.
Where a user submits the data where it gets stored on a table. Then the user submits the information to login.

Heres the problem. The information is not being stored. I don't have much experience with SQL, so doing what I usually do with something I don't know, I youtube it.
And that ultimately left me with this project here.


Note: Yes I did test the connection and it was connected. 

any help is appreciated

Register 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 Login_Database
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            
            string connection = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True";
            SqlConnection cn = new SqlConnection(connection);
            try
            {
                cn.Open();
            }
            catch
            {
                MessageBox.Show("Could not connect");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.Show();
            
            string connection = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True";
            SqlConnection cn = new SqlConnection(connection);
            try
            {
                cn.Open();
            }
            catch
            {
                MessageBox.Show("Could not login");
            }
            string username = textBox1.Text;
            string password = textBox2.Text;
            string sqlquery = ("SELECT * FROM User WHERE Username = '" + textBox1.Text + "'");
            sqlquery = "INSERT INTO [User] (Username, Password) VALUES ('" + textBox1.Text + "','" + textBox2.Text + ")";
            SqlCommand command = new SqlCommand(sqlquery, cn);
            command.Parameters.AddWithValue("Username", username);
            command.Parameters.AddWithValue("Password", password);
            command.Parameters.Clear();
            
            
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.Show();
            Form1 form1 = new Form1();
            form1.Hide();
        }
    }
}
Login 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 Login_Database
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string connection = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True";
            SqlConnection cn = new SqlConnection(connection);
            try
            {
                cn.Open();
            }
            catch
            {
                MessageBox.Show("Could not connect");
            }
            SqlCommand cmd = new SqlCommand("SELECT * FROM [USER]", cn);
            cmd.Connection = cn;
            SqlDataReader reader = null;
            reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                if (textBox1.Text == (reader["Username"].ToString()) && textBox2.Text == (reader["Password"].ToString()))
                {
                    MessageBox.Show("Logged In");
                }
            }
        }
    }
}







Attachment: project.rar

Answers (4)