Object must implement IConvertible

13 Jul 2004 15:29

While writing some ASP.NET, I got this error. I’ve just spent ten minutes figuring it out, so I thought I’d share.

I have the following code in my ASP.NET page. userName and password are the text box controls on my login page. This code is in the handler for the login button:

SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = connection.CreateCommand();
command.CommandText = "SELECT UserID FROM Users "
    + "WHERE UserName = @UserName "
    + "AND UserPassword = @UserPassword";
SqlParameter userNameParameter = command.Parameters.Add(
    "@UserName", SqlDbType.NVarChar, 50);
SqlParameter passwordParameter = command.Parameters.Add(
    "@UserPassword", SqlDbType.NVarChar, 50);

userNameParameter.Value = userName;
passwordParameter.Value = password;

connection.Open();
string userID = (string)command.ExecuteScalar();
connection.Close();

It causes the “Object must implement IConvertible” error on the call to ExecuteScalar.

The problem: you can’t pass TextBox controls as parameter values. The code should look like this:

userNameParameter.Value = userName.Text;
passwordParameter.Value = password.Text;

Doh!