Hello Term,
I have 4 tables, and table one is tblCLass which contain ClassId and ClassName, and table two is tblRegistrationFeesTable
which contain RegistrationId, Fees, and ClassId and the Table 3 is tblTerm which contain TermId and TermName,
and in table 4 is tblFeesPayment which contain FeesId,Payment date Student name,TermId, RegistrationId,ClassId, FeesPaid, Wallet and and I want to select ClassName and then it will fetch Fee by RegistrationId and whenever I select ClassName I want the existig fees to show in the approved textbox.
But when I select ClassName the fees doesn't show in the approved textbox.
public ActionResult GetStudentFees(int registrationId, int classId)
{
decimal SchoolFees = objBasicShoolDBEntities.tblRegistrationFees.Single(model => model.RegistrationId == registrationId && model.ClassId == classId).Fees;
return Json(SchoolFees, JsonRequestBehavior.AllowGet);
}
function GetTermlyFees(registrationId) {
$.ajax({
async: true,
type: 'GET',
dataType: 'JSON',
contentType: 'application/json; charset=utf-8',
data: { registrationId: registrationId},
url: '/Home/GetStudentFees',
success: function (data) {
$("#txtRegistrationFees").val(parseFloat(data).toFixed(2));
},
error: function () {
alert("There is some problem to get school fees.");
}
});
}

Prasad RaveendranRank #320 · 7.8K reputationPosted May 7, 2024, 8:40 PM EDT
try this approach
public ActionResult GetStudentFees(int registrationId, int classId) { // Find the registration fees for the given registrationId and classId var registrationFees = objBasicShoolDBEntities.tblRegistrationFees.SingleOrDefault(model => model.RegistrationId == registrationId && model.ClassId == classId); if (registrationFees != null) { // If a record is found, return the fees decimal schoolFees = registrationFees.Fees; return Json(schoolFees, JsonRequestBehavior.AllowGet); } else { // If no matching record is found, return an appropriate response return Json("Fees not found for the given registration and class.", JsonRequestBehavior.AllowGet); } }if its going to else part, we may need to look at the your entity relationship which you have configured
Jobin SRank #886 · 2.4K reputationPosted May 5, 2024, 8:43 PM EDT
Hi Emmanuel,
var registrationFee = objBasicShoolDBEntities.tblRegistrationFees .SingleOrDefault(rf => rf.RegistrationId == registrationId && rf.ClassId == classInfo.ClassId); return Json(new { Fee = registrationFee.Fees }, JsonRequestBehavior.AllowGet);in Ajax Function some modification
function GetTermlyFees(registrationId) { $.ajax({ async: true, type: 'GET', dataType: 'JSON', contentType: 'application/json; charset=utf-8', data: { registrationId: registrationId }, url: '/Home/GetStudentFees', success: function (data) { if (data.Error) { alert("Error: " + data.Error); } else { $("#txtRegistrationFees").val(parseFloat(data.Fee).toFixed(2)); } }, error: function (xhr, status, error) { console.error("AJAX error:", status, error); console.error("Response text:", xhr.responseText); alert("There is some problem to get school fees."); } }); }Jignesh KumarRank #91 · 22.9K reputationPosted May 5, 2024, 11:23 PM EDT
Hello Emmmanuel,
Are you getting error? if Yes
then you need to check this line,
decimal SchoolFees = objBasicShoolDBEntities.tblRegistrationFees.Single(model => model.RegistrationId == registrationId && model.ClassId == classId).Fees;in Linq the .single () extension method expects only single values for specified where clause. In the above case maybe you are not getting values or maybe more than one value for given registrationId and classId.
Jignesh KumarRank #91 · 22.9K reputationPosted May 6, 2024, 8:50 AM EDT
Have you checked registrationId and classId values from the method parameter,
if you are getting the below parameter,
registrationId =3 , classId =11
Then below query will execute in the SQL server and return the first row,
select Fees from Registration where registrationId =3 and classId =11 ;execute below query and check in sql server.
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted May 6, 2024, 10:06 AM EDT
Hello Kumar,
The query works perfectly when I execute in the SQL database, it fetch the first row
Jobin SRank #886 · 2.4K reputationPosted May 6, 2024, 11:18 AM EDT
Hi ,
if (registrationFee == null) Check this condition null criteia
I need to debug the code to see if the
classInfoobject has aClassIdproperty and check whether it's filled in correctly. I can go through the code line by line to make sure everything's as expectedEmmmanuel FIADUFERank #947 · 2.2K reputationPosted May 6, 2024, 11:27 AM EDT
Jobin yes classInfo has ClassId
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted May 6, 2024, 1:47 PM EDT
Please it is still not working, any other way would be appreciated
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted May 6, 2024, 6:08 PM EDT
With the debugger in the JavaScript function, is selecting the FeeId alright but the function says localhost: says there is some problem to get school fees
Jignesh KumarRank #91 · 22.9K reputationPosted May 6, 2024, 10:57 PM EDT
Hello Emmmanuel,
As per your last code snnipet, You are passing only registrationId from your AJAX call but in mvc controller you have two parameter
registrationId and TemlyFeesPaymentViewModel, so this one must change only the registrationId you need to pass in the controller method as well.
As per your table data registration Id is primary key so only one row will be there for one registrationId,
public ActionResult GetStudentFees(int registrationId, int classId) { decimal schoolFee= 0m; decimal SchoolFees = objBasicShoolDBEntities.tblRegistrationFees.SingleOrDefault(model => model.RegistrationId == registrationId); if(SchoolFees != 0) { schoolFee = SchoolFees.Fees } else { schoolFee = 0d; } return Json(new { Fees = schoolFee, JsonRequestBehavior.AllowGet); }Emmmanuel FIADUFERank #947 · 2.2K reputationPosted May 7, 2024, 4:25 PM EDT
Hello Kumar
even though the error is no longer showing, when I select the ClassName it doesn't fetch it associated fee, any other help would be greatly appreciated
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted May 8, 2024, 11:52 AM EDT
Am thinking may be the reason why the fees is not coming is how am selecting the class name, from above query
Jignesh KumarRank #91 · 22.9K reputationPosted May 8, 2024, 11:54 PM EDT
Hello Emmmanuel,
As you shared SQL server table data in that snippet there is no registrationId = 11. could you please check that table you are getting values for registrationId 11;
If not then please select other which are eixists in table and test it.
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted May 10, 2024, 4:46 PM EDT
Thank you team,
I change the class table and registration table to one instead of two and it is working the way I want it.
Ricepuritytest Blogger5 reputationPosted May 16, 2024, 4:58 AM EDT
Am thinking may be the reason why the fees is not coming is how am selecting the class name, from above query