I am trying to create an application for a market.
I want it to have a sign Up form for normal users so they just can see the products so I want a sign form to insert the users information.
Then when they try to log in, the log in form get the information from the sign up form.
Then open a normal user form I made it this far but I get this error
here is the login form
public OleDbConnection conect = new OleDbConnection();
public Login()
{
InitializeComponent();
conect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Mhamad\Desktop\form\Sign_Up.mdb;
Persist Security Info=False;";
}
private void Log_in_Click(object sender, EventArgs e)
{
conect.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = conect;
command.CommandText = "select * from Sign_Up where UserName='" + User_Name.Text + "' and Password='" + Password.Text + "'";
OleDbDataReader reader = command.ExecuteReader();
int count = 0;
while(reader.Read())
{
count = count + 1;
}
if (count==1)
{
MessageBox.Show("User Name and Password Are Correct ");
Admin admin = new Admin();
admin.Show();
this.Hide();
}
if (count > 1)
{
MessageBox.Show("Dublicated UserName And Password ");
}
else
{
MessageBox.Show("User Name and Password Are Not Correct ");
}
and this is the sign up form
public OleDbConnection conect = new OleDbConnection();
public SignUp_Form()
{
InitializeComponent();
conect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Mhamad\Desktop\form\Sign_Up.mdb;
Persist Security Info=False;";
}
private void button1_Click(object sender, EventArgs e)
{
try
{
Login lognin = new Login();
lognin.Show();
this.Hide();
conect.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = conect;
command.CommandText =
"insert into Sign_Up
([FirstName],[LastName], [UserName],[Password])
values('"
+ First.Text+ "','"
+ Last.Text + "','"
+ User.Text + "','"
+ Pass.Text + "')";
command.ExecuteNonQuery();
MessageBox.Show("Sign Up Succsaesful");
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
this is the error code :
OleDbDataReader reader = command.ExecuteReader();
System.Data.OleDb.OleDbException: 'The table 'Sign_Up' is already opened exclusively by another user, or it is already open through the user interface and cannot be manipulated programmatically.'
I also want to create a employer form and an admin that get its information from the same form is that possible ?
I am using access for the database
You didn't close the first connection what is opened on the first form. You should make use of the using block which will ensure the connection is closed and disposed correctly.
Login form
try
{
using (OleDbConnection conect = new OleDbConnection(connectionString))
{
conect.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = conect;
command.CommandText = "select * from Sign_Up where UserName='" + User_Name.Text + "' and Password='" + Password.Text + "'";
OleDbDataReader reader = command.ExecuteReader();
int count = 0;
while (reader.Read())
{
count = count + 1;
}
if (count == 1)
{
MessageBox.Show("User Name and Password Are Correct ");
Admin admin = new Admin();
admin.Show();
this.Hide();
}
if (count > 1)
{
MessageBox.Show("Dublicated UserName And Password ");
}
else
{
MessageBox.Show("User Name and Password Are Not Correct ");
}
}
}
catch (Exception ex)
{
MessageBox.Show($"Login form error: {ex.Message}");
}
The connection is automatically closed when the code exits the using block.
Sign up form
try
{
Login lognin = new Login();
lognin.Show();
this.Hide();
using (OleDbConnection conect = new OleDbConnection(connectionString))
{
conect.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = conect;
command.CommandText =
"insert into Sign_Up
([FirstName],[LastName], [UserName],[Password])
values('"
+ First.Text+ "','"
+ Last.Text + "','"
+ User.Text + "','"
+ Pass.Text + "')";
command.ExecuteNonQuery();
MessageBox.Show("Sign Up Succsaesful");
}
}
catch (Exception ex)
{
MessageBox.Show($"Sign up error:{ex.message}");
}
If you need employee and admin roles it's better to create two different forms what you can open after login.