cs_eyango
Messages postés45Date d'inscriptionjeudi 24 août 2006StatutMembreDernière intervention30 janvier 2014
-
6 sept. 2012 à 09:27
cs_eyango
Messages postés45Date d'inscriptionjeudi 24 août 2006StatutMembreDernière intervention30 janvier 2014
-
10 sept. 2012 à 13:00
Bonjour chers Tous,
je débute sous la plateforme Visual Studio 2010. et je developpe une appli web...
je souhaite importer dans ma base SQL SERVER un fichier de données Excel 2007:
l'erreur suivant est générée:
"The EntityKey property can only be set when the current value of the property is null."
voici mon code:
protected void ImporterButton_Click(object sender, EventArgs e)
{
ServiceCotisation SC = new ServiceCotisation();
DataAccessEF.Cotisation DaC = new DataAccessEF.Cotisation();
OleDbConnection conExcel = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\MURCAS;Extended Properties=""Excel 12.0 Xml;HDR=YES"";");
SqlConnection conSQL = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Bayard Joel\Documents\visual studio 2010\Projects\MurcasFasas2012\Presentation\App_Data\DatabaseMurcasFasas2012.mdf;Integrated Security=True;User Instance=True");
OleDbDataAdapter Da = new OleDbDataAdapter();
DataSet Ds = new DataSet();
OleDbCommand comm = new OleDbCommand("SELECT * FROM [RH$]", conExcel);
Da.SelectCommand = comm;
Da.Fill(Ds, "RH");
conSQL.Open();
DateTime DT = new DateTime();
DT = Calendar1.SelectedDate;
la classe qui définit la méthode AjouterCotisation:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccessEF;
using System.Data.Entity;
namespace BLL
{
public class ServiceCotisation
{
public void AjouterCotisation(Cotisation C)
{
using (MyDatabaseEntities1 MD = new MyDatabaseEntities1())
{
MD.AddToCotisation(C);
MD.SaveChanges();
}
}
}
}
la ligne qui génère l'erreur se trouve dans le "Model1.edmx":
public void AddToCotisation(Cotisation cotisation)
{
base.AddObject("Cotisation", cotisation);
}
fohaf
Messages postés60Date d'inscriptionvendredi 14 novembre 2003StatutMembreDernière intervention 6 septembre 20122 6 sept. 2012 à 10:47
Bonjour,
en regardant vite fait ton code (pense à utiliser la coloration syntaxique, il sera plus facile à lire), il me semble que tu essayes d'ajouter plusieurs fois le même objet Cotisation et tu as un conflit de clé primaire.
Essaye en créant ton objet cotisation dans ta boucle foreach:
foreach (DataRow row in Ds.Tables["RH"].Rows)
{
DataAccessEF.Cotisation DaC = new DataAccessEF.Cotisation();
DaC.Matricule = Convert.ToString(row[0]);
...
SC.AjouterCotisation(DaC);
...
}
cs_eyango
Messages postés45Date d'inscriptionjeudi 24 août 2006StatutMembreDernière intervention30 janvier 2014 10 sept. 2012 à 13:00
Salut fohaf j'ai encore un souci avec ce mm code voici la ligne qui cause le problÚme:
OleDbConnection conExcel = new
OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\MURCAS;Extended Properties=""Excel 12.0 Xml;HDR=YES"";");
je souhaiterais grace à un 'fileuplaod' choisir un fichier sur mon disque et renseigner le nom du fichier dans la chaine de connexion... mais lorsque je l applique comme suit:
OleDbConnection conExcel = new
OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:"+FileUpload1.FileName+";Extended Properties=Excel 12.0 Xml;HDR=YES;");
je reçois une erreur suivante: "Could not find installable ISAM."