-
Feb 16th, 2018, 01:59 PM
#1
Thread Starter
New Member
[RESOLVED] Help with registration form!
Hello! I created a registration and login form but I have a problem . When you created a user , you can create it again with different password .
Here is code of Registration 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.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace SyrX_Messenger
{
public partial class Reg : Form
{
private MySqlConnection conn;
private string server;
private string database;
private string uid;
private string password;
public Reg()
{
server = "localhost";
database = "syrx";
uid = "root";
password = "";
string connString;
connString = $"SERVER={server};DATABASE={database};UID={uid};PASSWORD={password}";
conn = new MySqlConnection(connString);
InitializeComponent();
}
private void Button2_Click(object sender, EventArgs e)
{
this.Hide();
var login = new Login();
login.Closed += (s, args) => this.Close();
login.Show();
}
private void Button1_Click(object sender, EventArgs e)
{
if (TextBox3.Text == "")
{
MessageBox.Show("Please approve your password");
}
else if (TextBox2.Text != TextBox3.Text)
{
MessageBox.Show("Passwords do not match");
}
else
{
string user = TextBox1.Text;
string pass = TextBox2.Text;
if (Register(user, pass))
{
MessageBox.Show($"User {user} successfully created");
}
else
{
MessageBox.Show($"User {user} did not created!");
}
}
}
public bool Register(string user, string pass)
{
string query = $"INSERT INTO users (id, username, password) VALUES ('', '{user}', '{pass}');";
try
{
if (OpenConnection())
{
MySqlCommand cmd = new MySqlCommand(query, conn);
try
{
cmd.ExecuteNonQuery();
return true;
}
catch (Exception ex)
{
return false;
}
}
else
{
conn.Close();
return false;
}
}
catch
{
conn.Close();
return false;
}
}
private bool OpenConnection()
{
try
{
conn.Open();
return true;
}
catch (MySqlException ex)
{
switch (ex.Number)
{
case 0:
MessageBox.Show("Can not connect to server");
break;
case 1045:
MessageBox.Show("Server error");
break;
}
return false;
}
}
}
}
I want to do that when you create a new account first it check if username exists and then create or give an error . How can I make that?
-
Feb 16th, 2018, 02:47 PM
#2
Re: Help with registration form!
When the button is pressed you currently check that the password confirmation has been entered correctly, then call Register (which simply adds a new user to the database).
As you want to avoid creating duplicate users you need to somehow detect if the new record would create a duplicate, and stop if it would.
An easy way to do that would be to query the database at the start of Register (using something like $"SELECT id FROM users WHERE username = {user}" ), and see if it returns any records... if it does, don't add the new user.
-
Feb 17th, 2018, 05:20 AM
#3
Thread Starter
New Member
Re: Help with registration form!
Can you help me with code ? I'm new at this. I don't understand all
-
Feb 17th, 2018, 05:46 AM
#4
Thread Starter
New Member
Re: Help with registration form!
I solved the problem with another way :P . I set the username as unique in mysql database . Thanks for help.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|