Loyalty program including Generate Voucher Code in asp.net webform

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;
    }
}`

New contributor

idunnowhy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật