I am a very beginner coder who is trying to make a table on C# in which I'm making a requisition slip which a person can enter data on and submit. When the data is submitted the data gets sent to a desired email but i keep getting errors like
"An unhandled exception occurred while processing the request."
How do I fix my code?
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Mail;
using Microsoft.Extensions.Logging;
namespace BestStore.Pages
{
public class PrivacyModel : PageModel
{
private readonly ILogger<PrivacyModel> _logger;
// This will hold the requisition data from the form
[BindProperty]
public List<RequisitionItem> RequisitionItems { get; set; } = new List<RequisitionItem>();
// Added property for the person's name requesting the items
[BindProperty]
public string? Name { get; set; } // Name of the person requesting, nullable
public PrivacyModel(ILogger<PrivacyModel> logger)
{
_logger = logger;
// Initialize the list of items with 21 empty rows (for 21 products)
for (int i = 0; i < 21; i++)
{
RequisitionItems.Add(new RequisitionItem());
}
}
// OnGet method is executed when the page is first loaded
public void OnGet()
{
}
// This method is triggered when the form is submitted
public IActionResult OnPostSubmit()
{
// Prepare the requisition details to send in the email
var requisitionDetails = "Requisition Slip\n\n";
requisitionDetails += "Name: " + (Name ?? "Not provided") + "\n";
requisitionDetails += "Date: " + DateTime.Now.ToString("yyyy-MM-dd") + "\n\n";
// Loop through the list of requisition items and add their details
foreach (var item in RequisitionItems)
{
requisitionDetails += $"Product: {(item.ProductName ?? "Not provided")}\n";
requisitionDetails += $"Weight: {(item.Weight ?? "Not provided")}\n";
requisitionDetails += $"Type: {(item.Type ?? "Not provided")}\n";
requisitionDetails += $"Quantity Requested: {(item.QuantityRequested?.ToString() ?? "Not provided")}\n";
requisitionDetails += $"Quantity Issued: {(item.QuantityIssued?.ToString() ?? "Not provided")}\n";
requisitionDetails += $"Remarks: {(item.Remarks ?? "Not provided")}\n";
requisitionDetails += "\n";
}
// Send the requisition details to the email address
SendEmail("[email protected]", "Requisition Slip", requisitionDetails);
// Redirect to a Thank You page or a success message
return RedirectToPage("Index"); // Make sure you have a ThankYou page or handle the response
}
// Method to send the requisition data via email using Gmail SMTP
private void SendEmail(string toEmail, string subject, string body)
{
try
{
var fromEmail = "[email protected]"; // Replace with your Gmail address
var appPassword = "your-app-password"; // Replace with the App Password generated for Gmail
var smtpClient = new SmtpClient("smtp.gmail.com")
{
Port = 587,
Credentials = new NetworkCredential(fromEmail, appPassword), // Gmail credentials
EnableSsl = true,
};
var mailMessage = new MailMessage
{
From = new MailAddress(fromEmail),
Subject = subject,
Body = body,
IsBodyHtml = false, // Set to true if you're sending HTML emails
};
mailMessage.To.Add(toEmail);
// Send the email
smtpClient.Send(mailMessage);
}
catch (Exception ex)
{
// Log the error or handle the failure case (e.g., show a message to the user)
_logger.LogError("Error sending email: " + ex.Message);
}
}
}
// Class to represent a single requisition item (row in the table)
public class RequisitionItem
{
// Nullable strings - No changes needed for these properties
public string? ProductName { get; set; } // Nullable string
public string? Weight { get; set; } // Nullable string
public string? Type { get; set; } // Nullable string
public string? Remarks { get; set; } // Nullable string
// Nullable integers for quantities
public int? QuantityRequested { get; set; } // Nullable integer
public int? QuantityIssued { get; set; } // Nullable integer
}
}
"An unhandled exception occurred while processing the request."
How do I fix my code?
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Mail;
using Microsoft.Extensions.Logging;
namespace BestStore.Pages
{
public class PrivacyModel : PageModel
{
private readonly ILogger<PrivacyModel> _logger;
// This will hold the requisition data from the form
[BindProperty]
public List<RequisitionItem> RequisitionItems { get; set; } = new List<RequisitionItem>();
// Added property for the person's name requesting the items
[BindProperty]
public string? Name { get; set; } // Name of the person requesting, nullable
public PrivacyModel(ILogger<PrivacyModel> logger)
{
_logger = logger;
// Initialize the list of items with 21 empty rows (for 21 products)
for (int i = 0; i < 21; i++)
{
RequisitionItems.Add(new RequisitionItem());
}
}
// OnGet method is executed when the page is first loaded
public void OnGet()
{
}
// This method is triggered when the form is submitted
public IActionResult OnPostSubmit()
{
// Prepare the requisition details to send in the email
var requisitionDetails = "Requisition Slip\n\n";
requisitionDetails += "Name: " + (Name ?? "Not provided") + "\n";
requisitionDetails += "Date: " + DateTime.Now.ToString("yyyy-MM-dd") + "\n\n";
// Loop through the list of requisition items and add their details
foreach (var item in RequisitionItems)
{
requisitionDetails += $"Product: {(item.ProductName ?? "Not provided")}\n";
requisitionDetails += $"Weight: {(item.Weight ?? "Not provided")}\n";
requisitionDetails += $"Type: {(item.Type ?? "Not provided")}\n";
requisitionDetails += $"Quantity Requested: {(item.QuantityRequested?.ToString() ?? "Not provided")}\n";
requisitionDetails += $"Quantity Issued: {(item.QuantityIssued?.ToString() ?? "Not provided")}\n";
requisitionDetails += $"Remarks: {(item.Remarks ?? "Not provided")}\n";
requisitionDetails += "\n";
}
// Send the requisition details to the email address
SendEmail("[email protected]", "Requisition Slip", requisitionDetails);
// Redirect to a Thank You page or a success message
return RedirectToPage("Index"); // Make sure you have a ThankYou page or handle the response
}
// Method to send the requisition data via email using Gmail SMTP
private void SendEmail(string toEmail, string subject, string body)
{
try
{
var fromEmail = "[email protected]"; // Replace with your Gmail address
var appPassword = "your-app-password"; // Replace with the App Password generated for Gmail
var smtpClient = new SmtpClient("smtp.gmail.com")
{
Port = 587,
Credentials = new NetworkCredential(fromEmail, appPassword), // Gmail credentials
EnableSsl = true,
};
var mailMessage = new MailMessage
{
From = new MailAddress(fromEmail),
Subject = subject,
Body = body,
IsBodyHtml = false, // Set to true if you're sending HTML emails
};
mailMessage.To.Add(toEmail);
// Send the email
smtpClient.Send(mailMessage);
}
catch (Exception ex)
{
// Log the error or handle the failure case (e.g., show a message to the user)
_logger.LogError("Error sending email: " + ex.Message);
}
}
}
// Class to represent a single requisition item (row in the table)
public class RequisitionItem
{
// Nullable strings - No changes needed for these properties
public string? ProductName { get; set; } // Nullable string
public string? Weight { get; set; } // Nullable string
public string? Type { get; set; } // Nullable string
public string? Remarks { get; set; } // Nullable string
// Nullable integers for quantities
public int? QuantityRequested { get; set; } // Nullable integer
public int? QuantityIssued { get; set; } // Nullable integer
}
}