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()
Nombre
is your primary key and as such you're trying to insert a duplicate, hence the error message to that effect. – Bob KaufmanDNI
? A unique one for each record? – Bob Kaufman