Click here to Skip to main content
15,308,404 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SaveChanges() doesn't insert any records to database
update code work but insert code don't work
my code :

var DBs2 = ConnectionTools.OpenConn();
                DBs2.Configuration.AutoDetectChangesEnabled = false;
                DBs2.Configuration.ValidateOnSaveEnabled = false;
var resList = CreatedSerials.Where(u => u.Item4 == VID).ToList();
                    foreach (var r in resList)
                    {
                        // if serial id ==0 => new then add it as new
                        if ( r.Item7 == 0)
                        {
                            try
                            {
                               
                                var purchasesItemSerials = new purchases_item_seriels()
                                {
                                    pitem_ID = pitem_ID,
                                    stitems_ID = r.Item1,
                                    pmain_ID = PurchasesID,
                                    pitem_virtualID = r.Item4,
                                    pis_CustomSerial = r.Item2,
                                    pis_ExpireDate = r.Item3,
                                    pis_Statues = 0,
                                    ss_StoreID = Convert.ToInt32(gridView1.GetRowCellValue(ItemImdex, "storeID")),
                                    Purchases_Price = Convert.ToDecimal(gridView1.GetRowCellValue(ItemImdex, "item_NetSmallestUnitPrice")),
                                };
                                ss.Add(purchasesItemSerials);
                               
                                
                            }
                            catch (Exception eeeeee)
                            {
                                Msg.Show("", eeeeee.ToString(), 0);return;
                            }
                        } else
                        {
                            var DBs350 = ConnectionTools.OpenConn();
                            var UpdateSerial = DBs350.purchases_item_seriels.Find(r.Item7);
                            UpdateSerial.pitem_ID = pitem_ID;
                            UpdateSerial.stitems_ID = r.Item1;
                            UpdateSerial. pmain_ID = PurchasesID;
                            UpdateSerial.pitem_virtualID = r.Item4;
                            UpdateSerial.pis_CustomSerial = r.Item2;
                            UpdateSerial.pis_ExpireDate = r.Item3;
                            UpdateSerial.pis_Statues = r.Item6;
                            UpdateSerial.ss_StoreID = 
                     Convert.ToInt32(gridView1.GetRowCellValue(ItemImdex, "storeID"));
                            UpdateSerial.Purchases_Price = Convert.ToDecimal(gridView1.GetRowCellValue(ItemImdex, "item_NetSmallestUnitPrice"));
                            DBs350.SaveChanges();
                        }
                        
                    }
                    try
                    {
                        DBs2.purchases_item_seriels.AddRange(ss);
                        DBs2.SaveChanges();
                    }
                    catch (Exception eeeeee)
                    {
                        Msg.Show("", eeeeee.ToString(), 0);return;
                    }


What I have tried:

i tried also :
DBs2.Configuration.AutoDetectChangesEnabled = true;
                DBs2.Configuration.ValidateOnSaveEnabled = true;


no data inserted , no errors appear


-- i tried :
int returnCode = DBs2.SaveChanges();

and returnCode = 0


-- i tried also that :

// if this serial is new
                    var NewSerialresList = CreatedSerials.Where(u => u.Item4 == VID && u.Item7 == 0).ToList();
                    if (NewSerialresList.Count() > 0)
                    {
                     
                        var ss = new List<purchases_item_seriels>();

                        foreach (var r in NewSerialresList)
                        {
                            try
                            {
                                mrsalesdbEntities DBs002 = new mrsalesdbEntities();
                                var purchasesItemSerials = new purchases_item_seriels()
                                {
                                    pitem_ID = pitem_ID,
                                    stitems_ID = r.Item1,
                                    pmain_ID = PurchasesID,
                                    pitem_virtualID = r.Item4,
                                    pis_CustomSerial = r.Item2,
                                    pis_ExpireDate = r.Item3,
                                    pis_Statues = 0,
                                    ss_StoreID = Convert.ToInt32(gridView1.GetRowCellValue(ItemImdex, "storeID")),
                                    Purchases_Price = Convert.ToDecimal(gridView1.GetRowCellValue(ItemImdex, "item_NetSmallestUnitPrice")),
                                };
                                //ss.Add(purchasesItemSerials);
                                DBs002.purchases_item_seriels.Add(purchasesItemSerials);
                                DBs002.SaveChanges();
                            }
                            catch (Exception ex)
                            {
                                Msg.Show("", ex.ToString(), 0);
                            }
                            
                        }

                        int CC = ss.Count();
                        
                    }
Posted
Updated 25-Nov-18 7:27am
v3
Comments
George Swan 25-Nov-18 2:15am
   
Have you checked that the variable 'ss' is not empty?
Golden Basim 25-Nov-18 4:55am
   
no 'ss' have 2 record ( count of the new records).
sajeetharan 25-Nov-18 7:27am
   
what is DBs350 here ?
Golden Basim 25-Nov-18 7:54am
   
public static mrsalesdbEntities OpenConn()
{
mrsalesdbEntities MrSalesContext = new mrsalesdbEntities();
MrSalesContext.ChangeDatabase
(
initialCatalog: myconn.database,
port: Convert.ToUInt32( myconn.port),
userId: myconn.uid,
password: myconn.password,
dataSource: myconn.server
);
return MrSalesContext;
}

var DBs350 = ConnectionTools.OpenConn();
Gerry Schmitz 25-Nov-18 10:47am
   
You should try some "pure" EF code, instead of your "framework" (which obviously has some issues).

And the "lazy man's" "var" approach does not provide for much "clarity" either.

(You can use "var"; then use VS to replace it with an explicit type definition.)
Golden Basim 25-Nov-18 13:25pm
   
i tried that and same result

// if this serial is new
var NewSerialresList = CreatedSerials.Where(u => u.Item4 == VID && u.Item7 == 0).ToList();
if (NewSerialresList.Count() > 0)
{


var ss = new List<purchases_item_seriels>();

foreach (var r in NewSerialresList)
{
try
{
//var DBs002 = ConnectionTools.OpenConn();
mrsalesdbEntities DBs002 = new mrsalesdbEntities();
var purchasesItemSerials = new purchases_item_seriels()
{
pitem_ID = pitem_ID,
stitems_ID = r.Item1,
pmain_ID = PurchasesID,
pitem_virtualID = r.Item4,
pis_CustomSerial = r.Item2,
pis_ExpireDate = r.Item3,
pis_Statues = 0,
ss_StoreID = Convert.ToInt32(gridView1.GetRowCellValue(ItemImdex, "storeID")),
Purchases_Price = Convert.ToDecimal(gridView1.GetRowCellValue(ItemImdex, "item_NetSmallestUnitPrice")),
};
//ss.Add(purchasesItemSerials);
DBs002.purchases_item_seriels.Add(purchasesItemSerials);
DBs002.SaveChanges();
}
catch (Exception ex)
{
Msg.Show("", ex.ToString(), 0);
}

}


}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900