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 Formusing 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");
}
}
}
}
}
Bryian TanPosted May 24, 2011, 12:45 AM
On the Register form, comment out
//string sqlquery = ("SELECT * FROM User WHERE Username = '" + textBox1.Text + "'");
and modify the code according to below.
string sqlquery = "INSERT INTO [User] (Username, Password) VALUES ('" + username + "','" + password + "')";
using (SqlCommand command = new SqlCommand(sqlquery, cn))
{
command.Parameters.AddWithValue("Username", username);
command.Parameters.AddWithValue("Password", password);
command.Parameters.Clear();
command.ExecuteNonQuery(); //you missing this in the code
cn.close();
}
You need to close the connection too after the execution, the code in the Form_Load is unnecessary. Please refer to this post to get the correct syntax.
http://www.willasrari.com/blog/using-sqlcommand-parameters-with-multiple-inserts/000199.aspx
Majid KamaliPosted May 23, 2011, 10:13 PM
3. Because you did not closed single quotation mark.
... textBox2.Text + ")";
It must be:
... textBox2.Text + "')";
BTW you don't need to put table name between brackets in INSERT command.
I recommend, open your sql database through VS and set username column as primary key.
Kevin CopePosted May 23, 2011, 8:45 PM
2." In register form, after this line:
string sqlquery = ("SELECT * FROM User WHERE Username = '" + textBox1.Text + "'");
you did not check whether username exists or not, and immediately change sqlquery string."
I'm confused
3. I tried that. And when I type in my user/pass I will get a error with the password
http://tinypic.com/r/euf3a9/7
But I fixed it by adding a '
sqlquery = "INSERT INTO [User] (Username, Password) VALUES ('" + textBox1.Text + "','" + textBox2.Text + "')";
^
When I debugged after that, it still didnt work
4. This should explain your questions
http://tinypic.com/r/2jadstz/7
5. Thanks
Majid KamaliPosted May 23, 2011, 6:41 PM
2. In register form, after this line:
string sqlquery = ("SELECT * FROM User WHERE Username = '" + textBox1.Text + "'");
you did not check whether username exists or not, and immediately change sqlquery string.
3. I think it is better to use ExecuteNonQuery() with that sqlquery string. It may work.
5. Where are textBox1 and textBox2? If they are in register form, why you need to open login form? you can show login form with different button. If they are in login form, you need to send data from login form to register form.
5. Nice and practical project.