0
votes

Using MVC 2 , MySQL

public ActionResult Register()
    {          
        string name = Request.Form["name"];
        string password = Request.Form["password"];

        ViewData["name"] = name;
        ViewData["password"] = password;
        // Pasa los datos al ViewData, hace el insert
        MySqlCommand cmd = new MySqlCommand("INSERT INTO tbl_alumnos(Nombre, Apellido) values('"+name+"','"+password+"')", cn);
        try
        {
            // Intenta conectarse e insertar los datos
            cn.Open();
            cmd.ExecuteNonQuery();
            return View("~/Views/Inicial/Inicial.aspx");
        }
        catch (Exception ex)
        {
            // si no puede, error
            ViewData["ex"] = ex;
            System.Diagnostics.Debug.WriteLine(ex);
            return View("Error");
        }
    }

This is my code from the controller And my error code is:

Lo sentimos; se produjo un error al procesar la solicitud. MySql.Data.MySqlClient.MySqlException (0x80004005): Duplicate entry '' for key 'PRIMARY' at MySql.Data.MySqlClient.MySqlStream.ReadPacket() at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId) at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int64& insertedId) at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force) at MySql.Data.MySqlClient.MySqlDataReader.NextResult() at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader() at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery() at MvcApplication1.Controllers.ValidarController.Register() in E:\Proyecto\TestsDByAjax\MvcApplication1\Controllers\ValidarController.cs:line 61

Line 61 says cmd.ExecuteNonQuery()

1
First, please don't store passwords in cleartext. This application might only be something you're learning with and may never make it into production, but break that very bad habit fast. Second, sanitize your inputs. Use parameters rather than creating SQL statements by concatenation so you avoid SQL injection. Finally, I'm guessing that Nombre is your primary key and as such you're trying to insert a duplicate, hence the error message to that effect.Bob Kaufman
Yes, this is a test application, umm, I have a primary Key called DNI, not NombreMiGu3X
Are you putting a value in DNI? A unique one for each record?Bob Kaufman
What is the mechanism of generating primary key?T.S.
No, im not putting any value to DNI, and I have it as NOT NULL on the database, maybe that's the problem?MiGu3X

1 Answers

0
votes

I mean, I have an answer already what is wrong, I just don't know what you want to do with it?

"INSERT INTO tbl_alumnos(Nombre, Apellido) values('"+name+"','"+password+"')"

In the statement above you inserting name and password into table. And you claiming that another field, namely DNI is the primary key on the table. You obviously don't insert anything into DNI. If the field DNI had identity insert you wouldn't have issue with the statement above. But you probably don't and this is why it complaining. So, if you would supply unique DNI, you would be OK