I have this code:
public partial class LogintoProfile : Form
{
public LogintoProfile()
{
InitializeComponent();
}
private void BoxPawo_TextChanged(object sender, EventArgs e)
{
//Boxen viser stjerner i stedet for text
BoxPawo.PasswordChar = '*';
//Maxlængde på Password
BoxPawo.MaxLength = 36;
}
private void BoxCopawo_TextChanged(object sender, EventArgs e)
{
//Boxen viser stjerner i stedet for text
BoxPawo.PasswordChar = '*';
//Maxlængde på Password
BoxPawo.MaxLength = 36;
}
private void Update_Click(object sender, EventArgs e)
{
string connStr = "server=localhost;user=root;database=p4_projekt;port=3306;password=Jeppesen95;charset=latin1;";
MySqlConnection conn = new MySqlConnection(connStr);
try
{
MessageBox.Show("Forbinder til databasen");
conn.Open();
string sql = "UPDATE p4_projekt.customer_table set First_name='" + BoxFornavn.Text + "',Last_name='" + BoxEfternavn.Text + "',Email='" + textBoxEmail.Text + "',Password=" + BoxPawo.Text + "WHERE Customer_id=@id";
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Console.WriteLine(rdr[0] + " -- " + rdr[1]); // [] kan ikke huske om det er array plads?!
}
rdr.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
conn.Close();
Console.WriteLine("Done.");
Console.ReadLine();
}
private void TankOp_Click(object sender, EventArgs e)
{
string connStr = "server=localhost;user=root;database=p4_projekt;port=3306;password=**********;charset=latin1;";
MySqlConnection conn = new MySqlConnection(connStr);
try
{
MessageBox.Show("Forbinder til databasen");
conn.Open();
string sql = "UPDATE p4_projekt.customer_table set Balance'" + Balance.Text + "WHERE Customer_id=@id";
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Console.WriteLine(rdr[0] + " -- " + rdr[1]); // [] kan ikke huske om det er array plads?!
}
rdr.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
conn.Close();
Console.WriteLine("Done.");
Console.ReadLine();
}
private void Back_Click(object sender, EventArgs e)
{
this.Hide();
Login.Login name = new Login.Login();
name.ShowDialog();
}
}
It's the Update_Click
method that causes me problems. I'm working on a Log-in application.
This page allows the user to edit their information. But to have an update
clause, you need a where
statement. It would be fine if the user knows its ID, because then I would figure it out. But in my case they don't.
I've read posts where you can use parameters, but I'm not sure about that. Here is the code for the log-in:
public partial class Login : Form
{
private string connStr;
private MySqlConnection conn;
public Login()
{
InitializeComponent();
}
private void connect_to_DB()
{
try
{
connStr = "server=localhost;user=root;database=p4_projekt;port=3306;password=**********;charset=latin1;";
conn = new MySqlConnection(connStr);
conn.Open();
}
catch (MySqlException e)
{
throw;
}
}
private bool login_validation(string email, string pass)
{
connect_to_DB();
MySqlCommand cmd = new MySqlCommand();
cmd.CommandText = "Select * from customer_table where Email=@email and Password=@pass";
cmd.Parameters.AddWithValue("@email", email);
cmd.Parameters.AddWithValue("@pass", pass);
cmd.Connection = conn;
MySqlDataReader login = cmd.ExecuteReader();
if (login.Read())
{
conn.Close();
return true;
}
else
{
conn.Close();
return false;
}
}
private void buttonSubmit_Click(object sender, EventArgs e)
{
string email = textBoxEmail.Text;
string pass = textBoxPass.Text;
if (email == "" || pass == "")
{
MessageBox.Show("Tomme felter, udfyld venligst begge felter");
return;
}
bool r = login_validation(email, pass);
if (r)
{
MessageBox.Show("Korrekte oplysninger");
this.Hide();
LogintoProfile name = new LogintoProfile();
name.ShowDialog();
}
else
MessageBox.Show("Forkerte oplysninger");
}
}