Reputation: 47
I intend to insert forms data into a data table but it's not working.
I have used Code first to update data table.
public partial class tmp2 : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.Tmembre",
c => new
{
Idmembre = c.Int(),
Nommembre = c.String(),
Prenommembre = c.String(),
Mailmembre = c.String(),
SRCImage = c.String(),
Idassociation = c.Int(nullable: false),
})
.PrimaryKey(t => t.Idmembre);
}
}
The identity is not the primary key.
In the global model I have :
public partial class Tmembre
{
[Key]
public int Idmembre { get; set; }
public string Nommembre { get; set; }
public string Prenommembre { get; set; }
public string Mailmembre { get; set; }
public string SRCImage { get; set; }
public int Idassociation { get; set; }
}
and to add a membre in the member table I do :
else if (Request["boutonmembre"] == "Ajouter")
{
if (myFile != null && myFile.ContentLength > 0)
{
//string fileNameApplication = System.IO.Path.GetFileName(NAMEtxtFileName.FileName);
string fileExtensionApplication = System.IO.Path.GetExtension(myFile.FileName);
// generating a random guid for a new file at server for the uploaded file
string newFile = Guid.NewGuid().ToString() + fileExtensionApplication;
// getting a valid server path to save
string filePath = System.IO.Path.Combine(Server.MapPath("Content\\themes\\image"), newFile);
if (myFile.FileName != String.Empty)
{
myFile.SaveAs(filePath);
}
}
bool formok = true;
Tmembre Membreajoute = new Tmembre();
Membreajoute.SRCImage = "\\themes\\image\\photo_identite.jpg";
if (Request["nom"] != "")
{
Membreajoute.Nommembre = Request["nom"];
}
else
{
formok = false;
}
if (Request["prenom"] != "")
{
Membreajoute.Prenommembre = Request["prenom"];
}
else
{
formok = false;
}
if (Request["mail"] != "")
{
Membreajoute.Mailmembre = Request["mail"];
}
else
{
formok = false;
}
if (formok == true)
{
//Sur la base du membre numéro 1
Membreajoute.Idassociation = int.Parse(Request["idassociation"].ToString());
int membremaxid = db.TmembresansEDMdb.Max(u => u.Idmembre);
Membreajoute.Idmembre = membremaxid + 1;
db.TmembresansEDMdb.Add(Membreajoute);
db.SaveChanges();
Association = db.dbTassociation.Find(Membreajoute.Idassociation);
ViewData["Nomassociation"] = Association.Nomassociation;
ViewData["Idassociation"] = Membreajoute.Idassociation;
mymodel.lstmembre = (from a in db.TmembresansEDMdb
where a.Idassociation == Membreajoute.Idassociation
select a).ToList();
}
There are no errors but the row is not inserted in table.
Upvotes: 0
Views: 1510
Reputation: 417
As per code, few things need to verify step by steps
Are you able to verify your db config
Hope this help.
Upvotes: 0
Reputation: 2842
Are you adding
Tmembre Membreajoute = new Tmembre();
to the database context?
Like:
db.Membreajoutes.Add(Membreajoute);
Upvotes: 1