0

I am using this code to update the database:

var connectionString = ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString;
string commandString = string.Empty;
using (OleDbConnection con = new OleDbConnection(connectionString))
{
    con.Open();
    OleDbCommand command = new OleDbCommand(commandString, con);
    commandString = "UPDATE accesscontrol SET enabled=@isEnabled WHERE proxyFor=@userNameInGrid AND currentlyLoggedOnUser=@userName";

    command.Parameters.AddWithValue("userName", Environment.UserName);
    command.Parameters.AddWithValue("isEnabled", tempPerson.isBool.ToString());
    command.Parameters.AddWithValue("userNameInGrid", tempPerson.Name);
    command.ExecuteNonQuery();
    command.Parameters.Clear();
}

1 Answer 1

2

Because you use empty commandString

First, you set:

string commandString = string.Empty;

And then you passed empty commandString to OleDbCommand, after that you set the value to variable commandString but not to OleDbCommand

OleDbCommand command = new OleDbCommand(commandString, con);
commandString = "UPDATE accesscontrol SET enabled=@isEnabled WHERE proxyFor=@userNameInGrid AND currentlyLoggedOnUser=@userName";

Change you code to:

commandString = "UPDATE accesscontrol SET enabled=@isEnabled WHERE proxyFor=@userNameInGrid AND currentlyLoggedOnUser=@userName";
OleDbCommand command = new OleDbCommand(commandString, con);

UPDATE

Also, you should add @ before the parameter name

command.Parameters.AddWithValue("@isEnabled", tempPerson.isBool.ToString());
command.Parameters.AddWithValue("@userNameInGrid", tempPerson.Name);
command.Parameters.AddWithValue("@userName", Environment.UserName);

Note: as @LarsTech mentioned, OleDB does not use named parameters correctly. The parameters have to be added in the same index order they appear in the SQL statement

Sign up to request clarification or add additional context in comments.

3 Comments

@softwareisfun Any news? Was it helpful?
OleDB does not use named parameters correctly. The parameters have to be added in the same index order they appear in the SQL statement, so @isEnabled should be first, etc.
@LarsTech Thanks, I added it to the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.