I am going to guess the issue is that I have two seperate queries inside one command. Is there a way to get around this?
The connection string is filled with example data but it connects correctly.
I'm also aware that it's not secure to put connection strings inside the actual file but this is for testing purposes at the moment.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace Multiple_forms
{
public partial class Register : Form
{
public Register()
{
InitializeComponent();
}
private void registerSubmitButton_Click(object sender, EventArgs e)
{
string myConnection = "Server=localhost;Database=Houses;Uid=user;Pwd=password;";
MySqlConnection myConn = new MySqlConnection(myConnection);
string selectQuery = "Select * from users where RegistrationKey = '" + this.regKeyTextBox.Text + ";" + "UPDATE users set UserName = '" + this.regUsernameTextBox.Text.ToLower() +
"', Password = '" + this.regPasswordTextBox.Text + "', Email ='" +
this.regEmailTextBox.Text + "' WHERE RegistrationKey = '" + this.regKeyTextBox.Text +
"';";
string inputPass = this.regPasswordTextBox.Text;
string inputPassConfirm = this.regPasswordConfirmTextBox.Text;
MySqlCommand selectCommand = new MySqlCommand(selectQuery, myConn);
MySqlDataReader myReader;
if (inputPass == inputPassConfirm)
{
myConn.Open();
myReader = selectCommand.ExecuteReader();
int regCount = 0;
while (myReader.Read())
{
regCount = regCount + 1;
}
if (regCount == 1)
{
MessageBox.Show("You have registered successfully!");
}
else
{
MessageBox.Show("Invalid registration key.");
}
}
else
{
MessageBox.Show("Passwords don't match.");
Thread.Sleep(2000);
this.Close();
}
}
}
}
Because you receive an Sql Syntax Error, I suspect the problems lays in your query selectQuery
. As you can see below, you do not close the quotation marks after this.regKeyTextBox.Text
.
string selectQuery = "Select * from users where RegistrationKey = '" + this.regKeyTextBox.Text + ";" + "UPDATE users set UserName = '" + this.regUsernameTextBox.Text.ToLower() +
"', Password = '" + this.regPasswordTextBox.Text + "', Email ='" +
this.regEmailTextBox.Text + "' WHERE RegistrationKey = '" + this.regKeyTextBox.Text +
"';";
Try changing the query to :
string selectQuery = "Select * from users where RegistrationKey = '" + this.regKeyTextBox.Text + "';" + "UPDATE users set UserName = '" + this.regUsernameTextBox.Text.ToLower() +
"', Password = '" + this.regPasswordTextBox.Text + "', Email ='" +
this.regEmailTextBox.Text + "' WHERE RegistrationKey = '" + this.regKeyTextBox.Text +
"';";