I would like to update/edit my user data in Employee table in access database.
When i complete the fields that i want to change (name , last name, etc.), it gives me data updated but when i refresh the table, the data hasn't changed - been updated.
Changes i want to perform for example - Change name from Luke to Taylor, etc.
Where have i gone wrong? Where is the mistake in the code and does my code for adding users to database somehow have influence my update code?
My code for adding users is almost the same as for the update, except for query, and it works fine.
private void button2_Click(object sender, EventArgs e)
{
try
{
command.Connection = myConnection;
command.CommandText = "Update Employee set Name = @Name, LastName = @LastName, UserName = @UserName, Password = @Password, E_mail = @E_mail, Address = @Address WHERE ID = @ID";
command.Parameters.AddWithValue("@ID", userID.Text);
command.Parameters.AddWithValue("@Name", name.Text);
command.Parameters.AddWithValue("@LastName", lastName.Text);
command.Parameters.AddWithValue("@UserName", userName.Text);
command.Parameters.AddWithValue("@Password", pass.Text);
command.Parameters.AddWithValue("@E_mail", email.Text);
command.Parameters.AddWithValue("@Address", address.Text);
myConnection.Open();
command.ExecuteNonQuery();
MessageBox.Show("User updated!");
myConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Code for adding user data
private void button1_Click(object sender, EventArgs e)
{
try
{
command.Connection = myConnection;
command.CommandText = "Insert into Employee (ID, Name, LastName, UserName, Password, E_mail, Address)" + "values (@ID, @Name, @LastName, @UserName, @Password, @E_mail, @Address)";
command.Parameters.AddWithValue("@ID", userID.Text);
command.Parameters.AddWithValue("@Name", name.Text);
command.Parameters.AddWithValue("@LastName", lastName.Text);
command.Parameters.AddWithValue("@UserName", userName.Text);
command.Parameters.AddWithValue("@Password", pass.Text);
command.Parameters.AddWithValue("@E_mail", email.Text);
command.Parameters.AddWithValue("@Address", address.Text);
myConnection.Open();
command.ExecuteNonQuery();
MessageBox.Show("User added!");
myConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Thanks for the replies and help
I still have no solution for this. I've tried so many things but i just don't get the right answer.
My current code
try
{
OleDbConnection myConnection = new OleDbConnection("\\DATABASE PATH");
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = myConnection;
cmd.CommandText = "UPDATE Employees SET Name = @Name, LastName = @LastName, UserName = @UserName, Password = @Password, E_mail = @E_mail, Address = @Address WHERE ID = @";
cmd.Parameters.AddWithValue("@ID", userID.Text);
cmd.Parameters.AddWithValue("@Name", name.Text);
cmd.Parameters.AddWithValue("@LastName", lastName.Text);
cmd.Parameters.AddWithValue("@UserName", userName.Text);
cmd.Parameters.AddWithValue("@Password", pass.Text);
cmd.Parameters.AddWithValue("@E_mail", eMail.Text);
cmd.Parameters.AddWithValue("@Address", address.Text);
myConnection.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("User successfully added.");
myConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}