Вопрос

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);
       }
Это было полезно?

Решение 2

Try the following in your update code:

command.CommandText = "UPDATE Employee SET [Name] = ?, LastName = ?, UserName = ?, [Password] = ?, [E_mail] = ?, Address = ? WHERE [ID] = ?";

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);
command.Parameters.AddWithValue("@ID", userID.Text);

The parameters must be in the order in which they appear in the CommandText. This answer was suggested by: Microsoft Access UPDATE command using C# OleDbConnection and Command NOT working

The reasons for this is outlined here: http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.parameters(v=vs.110).aspx

The OLE DB .NET Provider does not support named parameters for passing parameters to an SQL statement or a stored procedure called by an OleDbCommand when CommandType is set to Text. In this case, the question mark (?) placeholder must be used.

Другие советы

Its because your ID in where condition.

You are also changing/updating your ID through:

command.Parameters.AddWithValue("@ID", userID.Text);

This new ID is not found by compiler in Database since you kept where ID=@ID condition in your query.

When you just updates name and other fields then query becomes:

Update Employee set Name = 'Name', LastName = 'LastName', UserName = 'UserName', Password = 'Password', E_mail = 'E_mail', Address = 'Address' WHERE ID = ''";

Your ID might remain blank in that case.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top