user1587086
user1587086

Reputation:

Recordset to iterate through database table

I have an asp.net e-commerce application in which I am adding functionality of Discount:

if the totalPurchase amount >=200, then 5% disc, if it is >=500, then 7%.. Like that.

Problem is the discount % should be changed dynamically via Admin login. i.e. I am not allowed to write this code in code-behind.

if(totalPurchase>=200 && totalPurchase<700)
{ // code for 5% discount }

I am trying to implement recordet to iterate through database table Discount whose fields are..

DiscID -1

DiscPer -5

DiscAmount -200 

and so on..

Upvotes: 2

Views: 1627

Answers (2)

M. Ahmad Zafar
M. Ahmad Zafar

Reputation: 4939

I feel that you are missing a database column for storing your discount amount range like 200 to 700 etc. I am assuming the database table name to be Discounts and the range columns to be AmountFrom and AmountTo. Here is my solution for you:

OleDbConnection connect = new OleDbConnection();
connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Data.accdb;Persist Security Info=False;";
connect.Open();

OleDbCommand command = new OleDbCommand();
command.CommandText = "SELECT AmountFrom, AmountTo, DiscPer FROM Discounts";
command.Connection = connect;

OleDbReader reader = command.ExecuteReader();

while(reader.Read())
{   
    int from = int.Parse(reader['AmountFrom'].toString());
    int to = int.Parse(reader['AmountTo'].toString());
    int discount = int.Parse(reader['DiscPer'].toString());

    if(totalPurchase >= from && totalPurchase < to) {
        MessageBox.Show("You got a discount of " + discount + "%");
    }
}
connect.Close();

Upvotes: 1

chridam
chridam

Reputation: 103435

This SO question may get you a good approach to your discounting problem Database Design Brainstorming: Sale Prices

Upvotes: 0

Related Questions