Solved

promo code to calculate discount from event cost

Posted on 2010-11-10
2
918 Views
Last Modified: 2012-05-10
Hello,
I have a user control built on Visual Studio 2005 on VB.NET with a sql 2008 database on the back end.
The user control recognizes promo codes stored in a database table and have a hard-dollar amount coded to a field called "DiscountValue".

What I'm attempting to do is the following:
When a  user enters the correct promo code (ie: DISC10), the form recognizes this as a valid code and applies a $10.00 discount to the price of an event.

I can modify the tblPromoCode to include an event price and that might make the calculation more self-contained within the sub that calls for the promo code verification.

The code that calls the subroutine follows:
----------
Protected Sub btnVerifyPromos_Click(ByVal sender As Object, ByVal e As System.EventArgs)

       
               
        ' Creates myConnection as a new sqlconnection and sets it equal to DSN_PROD
        Dim myConnection As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("DSN_PROD").ConnectionString)
       
       
        myConnection.Open()

        ' Build a sql statement string    
        Dim query1 As String = "Select PromoCode FROM tblPromoCodes WHERE PromoCode = @PromoCode"

        ' Initialize the sqlCommand with the new sql string.  
        Dim Command1 As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(query1, myConnection)
       
        ' Dim txtPromoCode As New TextBox
        Dim txtPromoCode As TextBox = DirectCast(RSVPForm1.FindControl("txtPromoCode"), TextBox)

       
        Command1.Parameters.AddWithValue("@PromoCode", txtPromoCode.Text)

        Dim PromoCode As String = "'"


        'Create new parameters for the sqlCommand object and initialize them to the input values.    

        'Execute the command
        PromoCode = Command1.ExecuteScalar
        ' Display whether the page passed validation.
       

        '******* Calculate the event price if PromoCode isValid *********
       
        Dim lbl_message As Label = DirectCast(RSVPForm1.FindControl("lbl_message"), Label)
        If String.IsNullOrEmpty(PromoCode) Then
         
            lbl_message.Text = "Promo Code Accepted."
            'Response.Write("--" + PromoCode.ToString() + "Accepted")
        Else
            lbl_message.Text = "Promo Code Invalid."
            'Response.Write("-- Not Accepted")
        End If

        ' args.IsValid = False
       
        If Not String.IsNullOrEmpty(PromoCode) Then
         
            lbl_message.Text = "Promo Code Accepted."
            ' Response.Write("--" + PromoCode.ToString() + "Accepted")
        Else
            lbl_message.Text = "Promo Code Invalid."
            'Response.Write("-- Not Accepted")
        End If

       
        myConnection.Close()
       
    End Sub
-----------

The code that calls for the btnVerifyPromos_Click event follows:
---------------
<!--Promo Coupon call starts here /\/\/\/\/\/\/\/-->
  <tr>
  <td style="text-align: right; white-space: nowrap; height: 25px; background-color:#ededed">
<asp:Label ID="Label2" runat="server" Text="Promo Code:"></asp:Label>&nbsp;</td>
     
<td style="text-align: left; white-space: nowrap; height: 25px;">
    <asp:TextBox ID="txtPromoCode" runat="server"></asp:TextBox>
 </td>    
    <td style="text-align: left; white-space: nowrap; height: 25px;">&nbsp; </td>
   <td style="text-align: left; white-space: nowrap; height: 25px;">
<asp:Button ID="btnVerifyPromos" runat="server" OnClick="btnVerifyPromos_Click" Text="Verify"  />
     <asp:Label ID="lbl_message" runat="server" Text="" ></asp:Label>
 </td>      
    </tr>
-----------------
ANY help would be greatly appreciated.



0
Comment
Question by:Codeaddict7423
2 Comments
 
LVL 15

Accepted Solution

by:
Praveen Venu earned 500 total points
ID: 34110284
Chane the query to

Select PromoCode,DiscountValue FROM tblPromoCodes WHERE PromoCode = @PromoCode

Thenyou will get the discount value also in the same query
0
 

Author Comment

by:Codeaddict7423
ID: 34111290
praveenvenu,
Thank  you for the quick reply.
I modificed the query as you suggested.  Below, please find my sub code:
------
Protected Sub btnVerifyPromos_Click(ByVal sender As Object, ByVal e As System.EventArgs)

        ' Protected Sub btnVerifyPromos_Click(ByVal sender As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
       
        'Handles CVPromoCode.ServerValidate
               
        ' Creates myConnection as a new sqlconnection and sets it equal to DSN_PROD
        Dim myConnection As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("DSN_PROD").ConnectionString)
     
        myConnection.Open()

        ' Build a sql statement string    
        Dim query1 As String = "Select PromoCode, DiscountValue FROM tblPromoCodes WHERE PromoCode = @PromoCode"

        ' Initialize the sqlCommand with the new sql string.  
        Dim Command1 As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(query1, myConnection)
       
        ' Declare txtPromoCode As TextBox and find in the form.
        Dim txtPromoCode As TextBox = DirectCast(RSVPForm1.FindControl("txtPromoCode"), TextBox)
       
        Command1.Parameters.AddWithValue("@PromoCode", txtPromoCode.Text)

        Dim PromoCode As String = "'"

        'Create new parameters for the sqlCommand object and initialize them to the input values.    

        'Execute the command
        PromoCode = Command1.ExecuteScalar
       
        ' Display whether the page passed validation.
        Dim lbl_message As Label = DirectCast(RSVPForm1.FindControl("lbl_message"), Label)
        If String.IsNullOrEmpty(PromoCode) Then
         
            lbl_message.Text = "Promo Code Accepted."
            'Response.Write("--" + PromoCode.ToString() + "Accepted")
        Else
            lbl_message.Text = "Promo Code Invalid."
            'Response.Write("-- Not Accepted")
        End If

        ' args.IsValid = False
       
        If Not String.IsNullOrEmpty(PromoCode) Then
         
            lbl_message.Text = "Promo Code Accepted."
            ' Response.Write("--" + PromoCode.ToString() + "Accepted")
           
            ' Declare txtDiscountValue As TextBox and find in the form.    
            Dim txtDiscountValue As TextBox = RSVPForm1.FindControl("txtDiscountValue")
            ' Initialize the sqlCommand with the same sql string.  
            Dim Command2 As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(query1, myConnection)
           
            'Command2.Parameters.AddWithValue("@DiscountValue", txtDiscountValue.Text)
            Dim DiscountValue As String = "'"
           
            'Execute the command
            'DiscountValue = Command2.ExecuteScalar
           
            'Display amount of discount on txtDiscountValue textbox
            ' txtDiscountValue.Text = RSVPForm1.FindControl("txtDiscountValue")
           
        Else
            lbl_message.Text = "Promo Code Invalid."
            'Response.Write("-- Not Accepted")
        End If

         
        '******* Calculate the event price if PromoCode isValid *********
           myConnection.Close()
       
        'Dim txtNumberofTickets As TextBox = RSVPForm1.FindControl("txtNumberofTickets")
        'txtNumberofTickets.Focus()
               
    End Sub
-------
The call to this sub is as follows:
-------
<!--Promo Coupon call starts here /\/\/\/\/\/\/\/-->
 
 <tr>
  <td style="text-align: right; white-space: nowrap; height: 25px; background-color:#ededed">
<asp:Label ID="Label2" runat="server" Text="Promo Code:"></asp:Label>&nbsp;</td>
     
<td style="text-align: left; white-space: nowrap; height: 25px;">
    <asp:TextBox ID="txtPromoCode" Width="150px" runat="server"></asp:TextBox>  
 </td>    
   
 <td style="text-align: left; white-space: nowrap; height: 25px;">&nbsp; </td>
 
 <td style="text-align: left; white-space: nowrap; height: 25px;">
 

<asp:Button ID="btnVerifyPromos" runat="server" OnClick="btnVerifyPromos_Click" Text="Verify"  />

     <asp:Label ID="lbl_message" runat="server" Text="" ></asp:Label>
 </td>      
 
  </tr>
-----
What I'm  trying to achieve is this: I'm trying to display the amount of money discounted from an event ticket price and the total discounted ticket price in the total textbox.

Right now, the ticket price is  handled via JavaScript as follows:
------
<script language="JavaScript" type="text/javascript">

function RegTotal()
{
form = document.forms[0];
counter = 0;
form = document.forms[0];
            counter = 0;
            if ((form.txtNumberofTickets.value != "") || (form.txtNumberofTickets.value.length != 0)) {      
                  counter = form.txtNumberofTickets.value
            }
            
            form.Regcount.value = counter;
      
        form.Total.value = parseFloat(counter*25);
       
       


var obj = document.getElementById(hdnTotalId);

obj.value = form.Total.value;

//var obj1 = document.getElementById(Attendee_FNameId);

//obj.value below this line is what drives the second caculated total

//obj1.value = form.Attendee_FName.value;


            var obj = document.getElementById("ctl00_ctl00_cphPage_cphContent_SpecialControl_RSVPForm1_hdnTotal");
        //var obj1 = document.getElementById("ctl00_ctl00_cphPage_cphContent_SpecialControl_RSVPForm1_Attendee_FName");

            
            //obj.value = form.Total.value;
            //obj1.value = form.Attendee_FName.value;
            
            

//var p = calcTotal(obj);
//alert("Total Price = " + p);            
            

function calcTotal(obj) {
var discount = 0;

var price = obj.value;
var savings = obj.value * discount;
var total = price - savings;
return form.Total.value;

}
}

</script>
------
I'm  trying to perform this calculation in the sub that calls the promo code.

ANY help would be greatly appreciated.

0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now