Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am able to insert data into "BankAccount" table. But could not make it for the other two tables for which I created two services. I also watched this in SQL profiler but no action happens when i run the code for inserting data into the two other tables.
Please HELP!!!!
This is my Controller:
C#
[HttpPost]
        public JsonResult CreateNewUser(BankAccount newUser)
        {
            string message = string.Empty;
            if (ModelState.IsValid)
            {
                
                string pswdsalt = PassWord.GenerateSalt();
                string pswd = PassWord.EncodePassword(newUser.Password, 1, pswdsalt);
                newUser.CreatedDate = System.DateTime.Now;
                newUser.Deleted = "N";
                newUser.Password = pswd;
                newUser.PasswordSalt = pswdsalt;
                try
                {
                    db.BankAccounts.Add(newUser);
                    idGeneratorService.UpdateNewAcNo(newUser.AccountNo);
                    balanceService.CreateBalanceDetailsForNewUser(newUser.AccountNo);
                    db.SaveChanges();
                    message = "New User Created Successfully";
                }
                catch (Exception Ex)
                {
                    message = "Oops! Something wrong happened" + Ex.Message;
                }
                
            }
            else
            {
                message = "Oops! Something wrong happened.Please try again after sometime";
            }

            return Json(message, JsonRequestBehavior.AllowGet);
        }

and my two Service named "idGeneratorService" & "balanceService" :
C#
public void UpdateNewAcNo(string AcNo)
        {
            TransactionIdGenerator acnoGenerator = new TransactionIdGenerator();
            acnoGenerator.AcNo = AcNo;
            db.SaveChanges();
        }

public void CreateBalanceDetailsForNewUser(string AcNo)
        {
            dailybalance.Id = Guid.NewGuid();
            dailybalance.AcNo = AcNo;
            dailybalance.Date = System.DateTime.Now;
            dailybalance.Balance = 0;
            dailybalance.Deleted = "N";
            db.SaveChanges();
        }
Posted
Comments
Member 11579819 3-Jan-16 14:11pm    
Also let me know if I just need to update one column of one row out of thousands of rows.. What would be the best approach?

1 solution

C#
db.BankAccounts.Add(newUser);
//first save bank account 
db.SaveChanges();
//now database will generate primary key for you to use as foreign key for other tables
idGeneratorService.UpdateNewAcNo(newUser.AccountNo);
balanceService.CreateBalanceDetailsForNewUser(newUser.AccountNo);
 
Share this answer
 

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