currently I’m using the asp.net webform for this project. Below are the backend code for my member.aspx which is the loyalty program. It should get the customer’s current points balance using session (which works fine, i can get their points balance using session and from NoSQL database). But it cant generate the code which it does not shows the code on the screen, which I’m using label to show all the generated code(can be called: redeemed vouchers).And the points should be deducted based on the required points values, but it can’t also. I should save the new points balance to my database also right? and I should have ‘vouchers’ in my database under the customer table to save voucher codes also? or I can straightly save into session without database?
Please help me on this, I already spent 2 days to solve this problem.
I really have no clue to solve this. Please help guys…..
SYEntities db = new SYEntities();
private static readonly Random random = new Random();
private const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
`
protected void Page_Load(object sender, EventArgs e)
{
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value);
string name = ticket.Name;
// retrieve the member points from the database
Customer C = db.Customers.SingleOrDefault(x => x.cust_name == name);
if (C != null)
{
int points = (int)C.mem_point;
lblPoints.Text = "Your current points balance: " + points;
Session["points"] = points;
}
List<string> redeemedVouchers = GetRedeemedVouchersFromSession();
// Display redeemed vouchers on label
if (redeemedVouchers.Any())
{
lblRedeemedVouchers.Text = "Redeemed vouchers: " + string.Join(", ", redeemedVouchers);
}
else
{
lblRedeemedVouchers.Text = "No vouchers redeemed yet.";
}
}
protected void btnRedeem1_Click(object sender, EventArgs e)
{
Button btnRedeem = (Button)sender;
int pointsRequired = GetPointsRequired(btnRedeem.CommandArgument);
int customerPoints = (int)Session["points"];
if (customerPoints >= pointsRequired)
{
// Deduct points from customer's balance
customerPoints -= pointsRequired;
// Generate voucher code
string voucherCode = GenerateVoucherCode(pointsRequired);
// Save redeemed voucher
List<string> redeemedVouchers = GetRedeemedVouchersFromSession();
redeemedVouchers.Add(voucherCode);
SaveRedeemedVouchersToSession(redeemedVouchers);
// Update points balance in session
Session["points"] = customerPoints;
// Display success message
ScriptManager.RegisterStartupScript(this, GetType(), "voucherRedeemed", "alert('Voucher redeemed successfully.');", true);
}
else
{
// Not enough points
ScriptManager.RegisterStartupScript(this, GetType(), "notEnoughPoints", "alert('Your points are not enough to redeem this voucher.');", true);
}
}
private int GetPointsRequired(string voucherCommandArgument)
{
// Extract points required from command argument
return int.Parse(voucherCommandArgument);
}
private string GenerateVoucherCode(int pointsRequired)
{
// Prefix based on points required
string prefix;
switch (pointsRequired)
{
case 10:
prefix = "1OFF";
break;
case 50:
prefix = "5OFF";
break;
case 100:
prefix = "10OFF";
break;
case 150:
prefix = "15OFF";
break;
case 200:
prefix = "25OFF";
break;
case 250:
prefix = "30OFF";
break;
default:
throw new ArgumentException("Invalid points required.");
}
// Generate voucher code
string code = prefix + GenerateRandomString(6); // Prefix + 6 random characters
return code;
}
private string GenerateRandomString(int length)
{
// Generate random string of specified length
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[random.Next(s.Length)]).ToArray());
}
private List<string> GetRedeemedVouchersFromSession()
{
// Retrieve redeemed vouchers from session or return an empty list
return Session["redeemedVouchers"] as List<string> ?? new List<string>();
}
private void SaveRedeemedVouchersToSession(List<string> redeemedVouchers)
{
// Save redeemed vouchers to session
Session["redeemedVouchers"] = redeemedVouchers;
}
}`
idunnowhy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.