2

When I try to insert to my DataBase I got error Parameter '@email' must be defined my code:

 MySqlCommand cmd = new MySqlCommand("INSERT INTO users VALUES(@email,@password,@firstName,@LastName,@UserID,@address,@birthday,0)", con);    
cmd.Parameters.AddWithValue(@email, email);
            cmd.Parameters.AddWithValue(@password, password);
            cmd.Parameters.AddWithValue(@firstName, firstName);
            cmd.Parameters.AddWithValue(@LastName, LastName);
            cmd.Parameters.AddWithValue(@UserID,UserID);
            cmd.Parameters.AddWithValue(@address, address);
            cmd.Parameters.AddWithValue(@birthday, birthday);

            /*THE PROBLEM ISNT IN THE DATABASE,THE FOLOOW INSERT STATMENT WORK!"*/
           // MySqlCommand cmd = new MySqlCommand("INSERT INTO users VALUES('evyataresdxez','sdfs','sdfsdf','sdds',33,'sdfsdf','232',0)", con);


            con.Open();
            cmd.ExecuteNonQuery();
            cmd.Parameters.Clear();

I have no problem in the database/code because the code in the comment is work good. thanks

1 Answers1

3

You need to quote your parameter names. As it stands now, you're passing the parameter value as the parameter name:

        cmd.Parameters.AddWithValue("email", email);
        cmd.Parameters.AddWithValue("password", password);
        cmd.Parameters.AddWithValue("firstName", firstName);
        cmd.Parameters.AddWithValue("LastName", LastName);
        cmd.Parameters.AddWithValue("UserID", UserID);
        cmd.Parameters.AddWithValue("address", address);
        cmd.Parameters.AddWithValue("birthday", birthday);

This only compiled the way you had it because the @ symbol before a variable name is valid syntax and you happened to name your variables the same as the parameter names.

Community
  • 1
  • 1
Dark Falcon
  • 43,592
  • 5
  • 83
  • 98