Question

I am working for my thesis, in which I develop a GYM management system. I want the software display those members(students) whom fee paying date is expired, so they be told to pay their monthly fees. These are the tables definitions:

  1. members(students) m_id m_name m_photo etc

2.fees
f_id
f_amount
f_start_date
f_end_date

Here is the C# code:

// here i get the current fees id so that the loop should run till it
SqlCommand getCurrentF_id = new SqlCommand("select IDENT_CURRENT('fees')", con);
int GotCurrentF_id = Convert.ToInt32(getCurrentF_id.ExecuteScalar());
int feesId;
//here is loop to find "select DATEDIFF(day,'f_start_date','getdate())" of all members
for (feesId = 1; feesId <= GotCurrentF_id; feesId++)
{
//here i get the f_start_date from sql server
SqlCommand f_start_DateCommand = new SqlCommand("select f_start_date from fees where f_id=" + feesId + "", con);
string f_start_dateCommand1 = Convert.ToString(f_start_DateCommand.ExecuteScalar());
//finding the date different between f_start_date and now
SqlCommand dateDiffBetweenNowNdStartDate = new SqlCommand("select datediff(day,'" + f_start_dateCommand1 + "',getdate())", con);
int finalDateDiff = Convert.ToInt32(dateDiffBetweenNowNdStartDate.ExecuteScalar());

if (finalDateDiff > 30)
{
    da.SelectCommand = new SqlCommand("select * from members m, fees f where m.m_id=f.m_id and f.f_id="+feesId+"", con);
    da.Fill(ds);
    da.SelectCommand.ExecuteNonQuery();
    dataGridView1.DataSource = ds.Tables[0];

Let a member pays his fee today, so he should be ok. Unfortunately his previous f_start_date records will be compared with today's date and will be displayed in datagridview, the same member will be displayed as many times as he has paid fees. I want to compare his latest(current) f_start_date with current_date, to find out whether he is expired or not, but not his previous fees start dates that he even has already paid.

I would appreciate any help.

Was it helpful?

Solution

Seems like what you want (retrive fees depeding on dates and members) can be fully handle in sql statement, so manage yourself to do only one SqlCommand instead of doing many queries handled by C# code.

select * 
from 
    members m
    , fees f 
where 
    m.m_id=f.m_id 
    and datediff(day, f.f_start_date, getdate()) > 30
    

To retrieve all members with all fees linked but only if start date exceeded last 30 days (ofc, retrieve paid fees..)

I don't understood what is the condition that make one fee "the last fee" of a member.

If f_end_date is set if the fee is paid it would be something like :

select * 
from 
    members m
    , fees f 
where 
    m.m_id=f.m_id 
    and datediff(day, f.f_start_date, getdate()) > 30
    and f_end_date is null
    

If fees existing in fees are just current fees to paid, then manage only the last fee by date :

select * 
from 
    members m
    , ( SELECT m_id,max(f_start_date) as f_start_date
        FROM fees
        GROUP BY m_id) f_lastfees
    ,fees f
where 
    m.m_id=f.m_id 
    and f.m_id = f_lastfees.m_id
    and f.f_start_date = f_lastfees.f_start_date
    and datediff(day, f.f_start_date, getdate()) > 30 

Will retrieve only members who didn't paid the last fees over last 30 days

Just comment above if you need some help in sql, but anyway try to retrieve directly the information you need in sql statement, it's better for performance and makes code more readable.

Hope it helps ;)

EDITED

The new table fees will contains :

f_id
f_amount
f_creation_date --used to know if fee is not paid over last 30days
f_paid_date --used to know if fee is paid or not (equals null if not paid)

Final request looks like

select * 
from 
    members m
    ,fees f
where 
    m.m_id=f.m_id 
    and datediff(day, f.f_creation_date, getdate()) > 30 
    and f_paid_date is null

Will retrieve only merbers who didn't paid the last fees over last 30 days according to alter table made on fees table

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top