Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am developing a project. i have to give discount to the user based on the level of membership the user chooses at the time of registration. if the user selects silver then 10 % discount, if bronze then 20% and if gold then 30% discount. i am using the below code but i am not able to figure out how to use the if statement. textbox4 is showing the value from the database. if the value in textbox4 is silver then 10% discount or if the value in textbox4 is bronze then 20% or if the value in textbox4 is gold then 30%. please help.

What I have tried:

C#
decimal disc1=10;
decimal disc2 = 20;
decimal disc3=30;
string s, t;

string[] a = new string[5];
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=.\\sqlexpress;Initial Catalog=college_education;User ID=sa;Password=system;";
con.Open();

SqlCommand com = new SqlCommand("Select C.User_name,sum(C.qty*P.product_price) TAmount,CR.ADDRESS,CR.membership_level ML  from cart C,Customer_registration CR, prod P where C.product_id = P.id and C.user_name='" + Session["user"] + "' and status='ATC' and C.user_name=CR.user_name group by C.user_Name,CR.membership_level,CR.ADDRESS", con);

SqlDataReader sdr=com.ExecuteReader();
sdr.Read();

TextBox5.Text=sdr["TAmount"].ToString();
TextBox2.Text = sdr["User_name"].ToString();
TextBox4.Text = sdr["ML"].ToString();
TextBox3.Text = sdr["ADDRESS"].ToString();

if(TextBox4.Text=="silver")
{

}

else if (TextBox4.Text=="bronze")
{

}
else if (TextBox4.Text=="gold")
{


}
Posted
Updated 27-Jul-17 2:24am
v2
Comments
Michael_Davies 27-Jul-17 6:47am    
Do not use a TextBox for fixed selectable values, the user can type what they like, for instance if they type "Gold" there will be no discount, use a ComboBox.
sreeyush sudhakaran 27-Jul-17 7:04am    
Please improve your question, where you got stuck?
Member 12950401 27-Jul-17 7:06am    
sir...i am not able to write the if part of my code.

if(textbox4.text=silver)
{
code....
}
sreeyush sudhakaran 27-Jul-17 7:08am    
You need business logic?
sreeyush sudhakaran 27-Jul-17 7:07am    
TextBox4.Enabled = false;
Decimal DiscountAmount = 0;
Decimal TotalAmount ;
Decimal.TryParse(TextBox5.Text,out TotalAmount);

if(TextBox4.Text.Trim().ToLower()=="silver")
{
DiscountAmount = TotalAmount - (TotalAmount * 0.1);
}

else if (TextBox4.Text.Trim().ToLower()=="bronze")
{
DiscountAmount = TotalAmount - (TotalAmount * 0.2);
}
else if (TextBox4.Text.Trim().ToLower()=="gold")
{
DiscountAmount = TotalAmount - (TotalAmount * 0.3);
}

1 solution

Do this:
C#
double discount = 1.0;
string membership = textBox4.Text.Trim.ToLower();
switch (membership)
   {
   case "bronze": discount = 0.10; break;
   case "silver": discount = 0.20; break;
   case "gold": discount = 0.30; break;
   }

Then multiply the original price by (1.0 - discount) to get the new value.
 
Share this answer
 
Comments
HardikPatel.SE 27-Jul-17 10:41am    
Switch case is faster than if Condition.
OriginalGriff 27-Jul-17 10:56am    
Sometimes - it depends on the data and range of values.
However, in this case it's a whole load more readable! :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900