Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have student fee allocation form,once I allocate fee to student, I want to update the concession and final amount of the student

here is my post method of student fee allocation

What I have tried:

C#
[HttpPost]
        public ActionResult Index(CUSTOM_STUDENT_FEEALLOCTION_DATA CUSTOM_STUDENT_FEEALLOCTION_DATAObj)
        {

            var Student_FeeallocationDetail = CUSTOM_STUDENT_FEEALLOCTION_DATAObj.CUSTOM_STUDENT_FEEALLOCATION_DETAIL;
            var SelectStufeedetail = Student_FeeallocationDetail.Where(b => b.SELECTED_STUDENT == true).ToList();

            CUSTOM_STU_DETAIL stuentdetail = new CUSTOM_STU_DETAIL();
            try
            {
                foreach (var studentfeedatal in SelectStufeedetail)
                {
                    if (studentfeedatal.Registration_Id == null)
                    {
                        List<studentregdetail> regIdObj = new List<studentregdetail>();

                        regIdObj = (from r in db.Student_Registration
                                    join ra in db.Student_Re_Admission on r.Registration_Id equals ra.Registration_Id
                                    where r.Academic_Year_Id== studentfeedatal.Academic_Year_Id && r.Class_Id==studentfeedatal.Class_Id
                                    where ra.Promoted==false
                                    
                                    select new StudentRegDetail()
                                    {
                                        Registration_Id = r.Registration_Id,
                                        Admission_No = ra.Admission_No
                                    }).OrderBy(a => a.Registration_Id).ToList();

                        foreach (var regIds in regIdObj)
                        {
                            var fee_set = db.Student_Fee_Allocation.Where(a => a.Fee_Master_Id == studentfeedatal.Fee_Master_Id && a.Registration_Id == regIds.Registration_Id).ToList();
                            if (fee_set.Count == 0)
                            {
                                Student_Fee_Allocation Student_Fee_AlloctionObj = new Student_Fee_Allocation();
                                Student_Fee_AlloctionObj.Last_Updated_Date = DateTime.Now.ToUniversalTime();
                                Student_Fee_AlloctionObj.Last_Updated_By = CurrentUserID;
                                Student_Fee_AlloctionObj.Creation_Date = DateTime.Now.ToUniversalTime();
                                Student_Fee_AlloctionObj.Created_By = CurrentUserID;

                                //Student_Fee_AlloctionObj.Fee_Allocation_Id = studentfeedatal.Fee_Allocation_Id;
                                Student_Fee_AlloctionObj.Academic_Year_Id = studentfeedatal.Academic_Year_Id;
                                Student_Fee_AlloctionObj.Class_Id = studentfeedatal.Class_Id;
                                Student_Fee_AlloctionObj.Admission_No = regIds.Admission_No.ToString();
                                Student_Fee_AlloctionObj.Registration_Id = regIds.Registration_Id;
                                Student_Fee_AlloctionObj.Fee_Master_Id = studentfeedatal.Fee_Master_Id;
                                Student_Fee_AlloctionObj.Fee_Period_Id = studentfeedatal.Fee_Period_Id;
                                Student_Fee_AlloctionObj.Amount = studentfeedatal.Amount;
                                Student_Fee_AlloctionObj.Consession = studentfeedatal.Concession;
                                Student_Fee_AlloctionObj.Final_Amount = studentfeedatal.Final_Amount;
                                Student_Fee_AlloctionObj.Paid_Amount = 0;
                                Student_Fee_AlloctionObj.Balance = studentfeedatal.Final_Amount;
                                Student_Fee_AlloctionObj.Delete_Flag = false;

                                db.Student_Fee_Allocation.AddObject(Student_Fee_AlloctionObj);
                                db.SaveChanges();

                                
                            }
                            else
                            {
                                foreach (var feeset in fee_set)
                                {

                                    db.ObjectStateManager.ChangeObjectState(feeset, EntityState.Modified);
                                    db.SaveChanges();
                                }
                            }
                        }

                        ModelState.AddModelError("", "Fees Allocated Successfully.");
                    }
                    else
                    {
                        var fee_set = db.Student_Fee_Allocation.Where(a => a.Fee_Master_Id == studentfeedatal.Fee_Master_Id && a.Registration_Id == studentfeedatal.Registration_Id).ToList();                        
                        if (fee_set.Count == 0)
                        {
                            Student_Fee_Allocation Student_Fee_AlloctionObj = new Student_Fee_Allocation();
                            Student_Fee_AlloctionObj.Last_Updated_Date = DateTime.Now.ToUniversalTime();
                            Student_Fee_AlloctionObj.Last_Updated_By = CurrentUserID;
                            Student_Fee_AlloctionObj.Creation_Date = DateTime.Now.ToUniversalTime();
                            Student_Fee_AlloctionObj.Created_By = CurrentUserID;

                            //Student_Fee_AlloctionObj.Fee_Allocation_Id = studentfeedatal.Fee_Allocation_Id;
                            Student_Fee_AlloctionObj.Academic_Year_Id = studentfeedatal.Academic_Year_Id;
                            Student_Fee_AlloctionObj.Class_Id = studentfeedatal.Class_Id;
                            Student_Fee_AlloctionObj.Admission_No = studentfeedatal.Admission_No.ToString();
                            Student_Fee_AlloctionObj.Registration_Id = studentfeedatal.Registration_Id;
                            Student_Fee_AlloctionObj.Fee_Master_Id = studentfeedatal.Fee_Master_Id;
                            Student_Fee_AlloctionObj.Fee_Period_Id = studentfeedatal.Fee_Period_Id;
                            Student_Fee_AlloctionObj.Amount = studentfeedatal.Amount;
                            Student_Fee_AlloctionObj.Consession = studentfeedatal.Concession;
                            Student_Fee_AlloctionObj.Final_Amount = studentfeedatal.Final_Amount;
                            Student_Fee_AlloctionObj.Paid_Amount = 0;
                            Student_Fee_AlloctionObj.Balance = studentfeedatal.Final_Amount;
                            Student_Fee_AlloctionObj.Delete_Flag = false;

                            db.Student_Fee_Allocation.AddObject(Student_Fee_AlloctionObj);
                            db.SaveChanges();

                            ModelState.AddModelError("", "Fees Allocated Successfully.");
                        }
                        else
                        {
                            foreach (var feeset in fee_set)
                            {

                                db.ObjectStateManager.ChangeObjectState(feeset, EntityState.Modified);
                                db.SaveChanges();
                            }
                        }
                    }
                }
                FillViewBag_Search();
                return View();
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", "Error Allocating Fees." + ex.Message.ToString());
                FillViewBag_Search();
                return View();
            }
        }
Posted
Updated 15-Feb-16 19:46pm
v2
Comments
Hussain Javed 16-Feb-16 1:54am    
[HttpPost]
public ActionResult Index(CUSTOM_STUDENT_FEEALLOCTION_DATA CUSTOM_STUDENT_FEEALLOCTION_DATAObj)
{

var Student_FeeallocationDetail = CUSTOM_STUDENT_FEEALLOCTION_DATAObj.CUSTOM_STUDENT_FEEALLOCATION_DETAIL;
var SelectStufeedetail = Student_FeeallocationDetail.Where(b => b.SELECTED_STUDENT == true).ToList();

CUSTOM_STU_DETAIL stuentdetail = new CUSTOM_STU_DETAIL();
try
{
foreach (var studentfeedatal in SelectStufeedetail)
{
if (studentfeedatal.Registration_Id == null)
{
List<studentregdetail> regIdObj = new List<studentregdetail>();

regIdObj = (from r in db.Student_Registration
join ra in db.Student_Re_Admission on r.Registration_Id equals ra.Registration_Id
join c in db.Student_Fee_Allocation on r.Registration_Id equals c.Fee_Allocation_Id
where r.Academic_Year_Id== studentfeedatal.Academic_Year_Id && r.Class_Id==studentfeedatal.Class_Id
where ra.Promoted==false

select new StudentRegDetail()
{
Registration_Id = r.Registration_Id,
Admission_No = ra.Admission_No,
Fee_Allocation_Id = c.Fee_Allocation_Id
}).OrderBy(a => a.Registration_Id).ToList();

foreach (var regIds in regIdObj)
{
var fee_set = db.Student_Fee_Allocation.Where(a => a.Fee_Master_Id == studentfeedatal.Fee_Master_Id && a.Registration_Id == regIds.Registration_Id).ToList();
if (fee_set.Count == 0)
{
Student_Fee_Allocation Student_Fee_AlloctionObj = new Student_Fee_Allocation();
Student_Fee_AlloctionObj.Last_Updated_Date = DateTime.Now.ToUniversalTime();
Student_Fee_AlloctionObj.Last_Updated_By = CurrentUserID;
Student_Fee_AlloctionObj.Creation_Date = DateTime.Now.ToUniversalTime();
Student_Fee_AlloctionObj.Created_By = CurrentUserID;

//Student_Fee_AlloctionObj.Fee_Allocation_Id = studentfeedatal.Fee_Allocation_Id;
Student_Fee_AlloctionObj.Academic_Year_Id = studentfeedatal.Academic_Year_Id;
Student_Fee_AlloctionObj.Class_Id = studentfeedatal.Class_Id;
Student_Fee_AlloctionObj.Admission_No = regIds.Admission_No.ToString();
Student_Fee_AlloctionObj.Registration_Id = regIds.Registration_Id;
Student_Fee_AlloctionObj.Fee_Master_Id = studentfeedatal.Fee_Master_Id;
Student_Fee_AlloctionObj.Fee_Period_Id = studentfeedatal.Fee_Period_Id;
Student_Fee_AlloctionObj.Amount = studentfeedatal.Amount;
Student_Fee_AlloctionObj.Consession = studentfeedatal.Concession;
Student_Fee_AlloctionObj.Final_Amount = studentfeedatal.Final_Amount;
Student_Fee_AlloctionObj.Paid_Amount = 0;
Student_Fee_AlloctionObj.Balance = studentfeedatal.Final_Amount;
Student_Fee_AlloctionObj.Delete_Flag = false;

db.Student_Fee_Allocation.AddObject(Student_Fee_AlloctionObj);
db.SaveChanges();


}
[no name] 16-Feb-16 2:35am    
Can you tell us which part of your is not working?
Hussain Javed 16-Feb-16 3:35am    
first it checks if any fee.count() is there,else it allocates fee.otherwise there is feeallocation_id is there then it should be updated
else
{
string str = "Update Student_Fee_Allocation Set Consession='" + studentfeedatal.Concession + "',Final_Amount='" + studentfeedatal.Final_Amount + "' Where Fee_Allocation_Id='" + studentfeedatal.Fee_Allocation_Id + "'";
var result = db.ExecuteStoreCommand(str);
db.SaveChanges();
}
[no name] 16-Feb-16 3:49am    
Are you getting any value in result variable? If yes what is that.

Make sure that Fee_allocation_id is correct value. if Fee_Allocation_Id is of integer type then just append the value:

string str = "Update Student_Fee_Allocation Set Consession='" + studentfeedatal.Concession + "',Final_Amount='" + studentfeedatal.Final_Amount + "' Where Fee_Allocation_Id=" + studentfeedatal.Fee_Allocation_Id;

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