Tristan McPherson
Tristan McPherson

Reputation: 377

Failed to convert parameter value from SqlParameter to String

I get the error:

Failed to convert parameter value from SqlParameter to String.

I pass in:

TestUsername,
TestPassword,
TestFirstName,
TestLastName,
[email protected]

Code:

        try
        {

            Console.WriteLine("Inputs: Username, Password, First Name, Last Name, Email");
            Console.WriteLine("Username: ");
            string usernameInput = Console.ReadLine();
            Console.WriteLine("Password: ");
            string passwordInput = Console.ReadLine();
            Console.WriteLine("First Name: ");
            string firstNameInput = Console.ReadLine();
            Console.WriteLine("Last Name: ");
            string lastNameInput = Console.ReadLine();
            Console.WriteLine("Email: ");
            string emailInput = Console.ReadLine();

            SqlCommand createLogin = new SqlCommand("INSERT INTO [dbo].[Users] VALUES ('@username', '@password', '@firstname', '@lastname', '@email')", myConnection.SqlConnection);
            SqlParameter usernameParam = createLogin.Parameters.Add("@username", SqlDbType.VarChar, 40);
            SqlParameter passwordParam = createLogin.Parameters.Add("@password", SqlDbType.VarChar, 50);
            SqlParameter firstNameParam = createLogin.Parameters.Add("@firstname", SqlDbType.VarChar, 40);
            SqlParameter lastNameParam = createLogin.Parameters.Add("@lastname", SqlDbType.VarChar, 40);
            SqlParameter emailParam = createLogin.Parameters.Add("@email", SqlDbType.VarChar, 40);

            usernameParam.Value = usernameInput;
            passwordParam.Value = passwordInput;
            firstNameParam.Value = firstNameInput;
            lastNameParam.Value = lastNameInput;
            emailParam.Value = emailParam;


            createLogin.ExecuteNonQuery();
            Console.WriteLine("Username: {0} with Password: {1} created.", usernameInput, EncDec.Decrypt(passwordInput, "testEncryption"));
        }
        catch (Exception CreateLoginException)
        {
            Console.WriteLine(CreateLoginException);
        }

Upvotes: 1

Views: 8641

Answers (2)

John Woo
John Woo

Reputation: 263883

Your declaration of parameter and setting of values are fine. The only problem with your code is that, you have enclosed the parameters in a single quote which makes it a string and not a parameter anymore. Remove the single quote around your parameter in your query,

SqlCommand createLogin = new SqlCommand(
                 "INSERT INTO [dbo].[Users] 
                  VALUES (@username, @password, @firstname, @lastname, @email",
                 myConnection.SqlConnection);

Command object automatically handles it for you.

Upvotes: 8

iefpw
iefpw

Reputation: 7062

It should be

createLogin.Parameters.AddwithValue("@parameter", parametervalue)

and delete rest of the parameter code.

You don't need to get that complicated use AddWithValue instead of Add(

Upvotes: 2

Related Questions