Reputation: 73
i'm having some trouble with a program i'm trying to make, this is intended to check a mysql database to see if a user of that name already exits, if it doesn't, it should create one, and if it does, it should show an error prompt.
There are no compile errors and it's getting to the end of the code, telling me "user was created" but there is no data being added to the database, not sure where i'm going wrong here, thank you!
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 System.Net;
using MySql.Data.MySqlClient;
namespace Oswald
{
public partial class frmRegister : Form
{
public frmRegister()
{
InitializeComponent();
}
private void btnRegister_Click(object sender, EventArgs e)
{
String mysqlConnectionString = "Server=sql3.freesqldatabase.com;Database=sql367701;UID=sql367701;Password=*****;Port=3306";
MySqlConnection connection = new MySqlConnection(mysqlConnectionString);
connection.Open();
string query = "SELECT * FROM UserInfo WHERE Username='" + txtUsername.Text + "'";
bool exists = false;
MySqlCommand cmd = new MySqlCommand(query, connection);
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
if(txtUsername.Text == reader.GetString("Username"))
{
MessageBox.Show("User Already Exits", "Oswald", MessageBoxButtons.OK, MessageBoxIcon.Error);
exists = true;
}
else { exists = false; }
}
reader.Close();
if (exists == false)
{
query = "INSERT INTO UserInfo (Username, Password, Email, IP) VALUES (@username, @password, @ip);";
string ip = new WebClient().DownloadString(@"http://icanhazip.com").Trim();
using (MySqlCommand cmd2 = new MySqlCommand(query, connection))
{
cmd.Parameters.AddWithValue("@username", txtUsername.Text);
cmd.Parameters.AddWithValue("@password", txtPassword.Text);
cmd.Parameters.AddWithValue("@email", txtEmail.Text);
cmd.Parameters.AddWithValue("@ip", ip);
cmd.ExecuteNonQuery();
}
MessageBox.Show("Account Created!", "Oswald", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Close();
}
}
}
}
Upvotes: 0
Views: 91
Reputation: 11
also, you can use shortly version on your code ;)
string query = "SELECT COUNT(*) FROM UserInfo WHERE Username='" + txtUsername.Text + "'";
MySqlCommand cmd = new MySqlCommand(query, connection);
if (cmd.ExecuteScalar()>0)
{
MessageBox.Show("User Already Exits", "Oswald", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
query = "INSERT INTO UserInfo (Username, ...
or maybe you add unique index for UserInfo.
Upvotes: 0
Reputation: 6590
you are missing email in insert statement. It should be cmd2
. you are querying on cmd
query = "INSERT INTO UserInfo (Username, Password, Email, IP) VALUES (@username, @password, @email, @ip)";
using (MySqlCommand cmd2 = new MySqlCommand(query, connection))
{
cmd2.Parameters.AddWithValue("@username", txtUsername.Text);
cmd2.Parameters.AddWithValue("@password", txtPassword.Text);
cmd2.Parameters.AddWithValue("@email", txtEmail.Text);
cmd2.Parameters.AddWithValue("@ip", ip);
int rows = cmd.ExecuteNonQuery();
if(rows > 0)
MessageBox.Show("insert OK...");
}
Upvotes: 1
Reputation: 79
I think you are using cmd instead of cmd2 while you are saving the details to the database. :)
Upvotes: 1