Click here to Skip to main content
16,016,263 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!!

I have a data-table which has following structure
Contribution ||  MonthNum  || MonthName ||   startdate  ||  Enddate

900          ||     1      ||  January  ||   1/1/2009  ||  3/31/2009 
1000         ||     4      ||    April  ||  4/1/2009   ||  3/31/2010 
852          ||     12     ||  December ||  12/1/2008  ||  12/31/2008


What i need to do is fill another datatable For each month with the values given in this datatable.

for example contribution for MonthNum 1 is 900 now for MonthNum 2,3 it should be 900 then from 4 upto 11 it should be 1000.

Please tell me how to do this..
Posted
Updated 26-Jul-12 1:41am
v2

I would create a class to handle this... However I do have one question, in the original table the dates seem... odd you have a start and end date along with the month, start and enddate would be more useful as your december is actually in 2008.

Given the following Class:
C#
//Datatable to edit
DataTable ContributionInfo = new DataTable();

//Entry Point
public FiscalYear()
{
    SetupFiscalDataTable();
}

//Table Setup operations
private void SetupFiscalDataTable()
{
    ContributionInfo.Clear();
    ContributionInfo.Columns.Clear();
    ContributionInfo.Columns.Add("Month");
    ContributionInfo.Columns.Add("Fiscal");
    ContributionInfo.Columns.Add("Name");
    ContributionInfo.Columns.Add("Value");

    DataRow tmpRow = ContributionInfo.NewRow();
    tmpRow["Month"] = 1;
    tmpRow["Fiscal"] = 10;
    tmpRow["Name"] = "January";
    tmpRow["Value"] = 0;
    ContributionInfo.Rows.Add(tmpRow);

    DataRow tmpRow2 = ContributionInfo.NewRow();
    tmpRow2["Month"] = 2;
    tmpRow2["Fiscal"] = 11;
    tmpRow2["Name"] = "February";
    tmpRow["Value"] = 0;
    ContributionInfo.Rows.Add(tmpRow2);

    DataRow tmpRow3 = ContributionInfo.NewRow();
    tmpRow3["Month"] = 3;
    tmpRow3["Fiscal"] = 12;
    tmpRow3["Name"] = "March";
    tmpRow["Value"] = 0;
    ContributionInfo.Rows.Add(tmpRow3);

    DataRow tmpRow4 = ContributionInfo.NewRow();
    tmpRow4["Month"] = 4;
    tmpRow4["Fiscal"] = 1;
    tmpRow4["Name"] = "April";
    tmpRow["Value"] = 0;
    ContributionInfo.Rows.Add(tmpRow4);

    DataRow tmpRow5 = ContributionInfo.NewRow();
    tmpRow5["Month"] = 5;
    tmpRow5["Fiscal"] = 2;
    tmpRow5["Name"] = "May";
    tmpRow["Value"] = 0;
    ContributionInfo.Rows.Add(tmpRow5);

    DataRow tmpRow6 = ContributionInfo.NewRow();
    tmpRow6["Month"] = 6;
    tmpRow6["Fiscal"] = 3;
    tmpRow6["Name"] = "June";
    tmpRow["Value"] = 0;
    ContributionInfo.Rows.Add(tmpRow6);

    DataRow tmpRow7 = ContributionInfo.NewRow();
    tmpRow7["Month"] = 7;
    tmpRow7["Fiscal"] = 4;
    tmpRow7["Name"] = "July";
    tmpRow["Value"] = 0;
    ContributionInfo.Rows.Add(tmpRow7);

    DataRow tmpRow8 = ContributionInfo.NewRow();
    tmpRow8["Month"] = 8;
    tmpRow8["Fiscal"] = 5;
    tmpRow8["Name"] = "August";
    tmpRow["Value"] = 0;
    ContributionInfo.Rows.Add(tmpRow8);

    DataRow tmpRow9 = ContributionInfo.NewRow();
    tmpRow9["Month"] = 9;
    tmpRow9["Fiscal"] = 6;
    tmpRow9["Name"] = "September";
    tmpRow["Value"] = 0;
    ContributionInfo.Rows.Add(tmpRow9);

    DataRow tmpRow10 = ContributionInfo.NewRow();
    tmpRow10["Month"] = 10;
    tmpRow10["Fiscal"] = 7;
    tmpRow10["Name"] = "October";
    tmpRow["Value"] = 0;
    ContributionInfo.Rows.Add(tmpRow10);

    DataRow tmpRow11 = ContributionInfo.NewRow();
    tmpRow11["Month"] = 11;
    tmpRow11["Fiscal"] = 8;
    tmpRow11["Name"] = "November";
    tmpRow["Value"] = 0;
    ContributionInfo.Rows.Add(tmpRow11);

    DataRow tmpRow12 = ContributionInfo.NewRow();
    tmpRow12["Month"] = 12;
    tmpRow12["Fiscal"] = 9;
    tmpRow12["Name"] = "December";
    tmpRow["Value"] = 0;
    ContributionInfo.Rows.Add(tmpRow12);
}
private void ReSetupFiscalDataTable()
{
    for (int i = 0; i < ContributionInfo.Rows.Count; i++)
    {
        ContributionInfo.Rows[i]["Value"] = 0;
    }
}

//Returns Fiscal Year calculation
public DataTable GetFiscalDate(int sFiscalYear, DataTable sSentInfo)
{
    string filterExp;
    string sortExp;


    for (int i = 0; i < ContributionInfo.Rows.Count; i++)
    {
        int tmpContribution = 0;
        int tmpMonth = Convert.ToInt16(ContributionInfo.Rows[i]["Month"]);
        int tmpYear = sFiscalYear;
        if (i <= 2) { tmpYear = sFiscalYear + 1; } //Jan - Mar will be in next year

        filterExp = "'" + tmpMonth + "/2/" + tmpYear + "' > startdate and '" + tmpMonth + "/2/" + tmpYear + "' < enddate";
        sortExp = "startdate";
        DataRow[] tmpMonthtable;
        tmpMonthtable = sSentInfo.Select(filterExp, sortExp);
        for (int z = 0; z < tmpMonthtable.Length; z++)
        {
            tmpContribution += Convert.ToInt16(tmpMonthtable[z][0]);
        }

        ContributionInfo.Rows[i]["Value"] = tmpContribution;
    }

    return ContributionInfo;
}
//Returns True Year calculation
public DataTable GetTrueDate(int sTrueYear, DataTable sSentInfo)
{
    string filterExp;
    string sortExp;


    for (int i = 0; i < ContributionInfo.Rows.Count; i++)
    {
        int tmpContribution = 0;
        int tmpMonth = Convert.ToInt16(ContributionInfo.Rows[i]["Month"]);
        int tmpYear = sTrueYear;

        filterExp = "'" + tmpMonth + "/2/" + tmpYear + "' > startdate and '" + tmpMonth + "/2/" + tmpYear + "' < enddate";
        sortExp = "startdate";
        DataRow[] tmpMonthtable;
        tmpMonthtable = sSentInfo.Select(filterExp, sortExp);
        for (int z = 0; z < tmpMonthtable.Length; z++)
        {
            tmpContribution += Convert.ToInt16(tmpMonthtable[z][0]);
        }

        ContributionInfo.Rows[i]["Value"] = tmpContribution;
    }

    return ContributionInfo;
}


You could use this class to do two different entry points, one would give you the Fiscal year contributions (ie given a fiscal year of 2009 you would get info from April 2009 to March 2010) and the other would give you the true year Jan-Dec. Also, if you have over lap it would add them up.

The Key bit of code here that you are looking for is this:
C#
filterExp = "'" + tmpMonth + "/2/" + tmpYear + "' > startdate and '" + tmpMonth +         "/2/" + tmpYear + "' < enddate";
sortExp = "startdate";
DataRow[] tmpMonthtable;
tmpMonthtable = sSentInfo.Select(filterExp, sortExp);

This piece will allow you to select rows from your Data Table that are relevant and sort them if you want. Using this on a "for" statement with a row for each month allows you to pull out the contribution for a specific month and apply it however you want.

As a note, this code was modified from a similar project I recently worked on.
 
Share this answer
 
v2
Comments
ujjwal uniyal 27-Jul-12 2:21am    
Thanks Dan. it is what i was looking for. I think it will do the work for me.. :) thanks again
Dan Steuer 27-Jul-12 8:24am    
Glad it helped.
create table having months nos.

tablename=tblmonth
monthnos  actualmonno monthnm
-----------------------------
1     4     apr
2     5     may
3     6     june
.     .
.     .
.     .
12    3     march


Now join it with your table,

SQL
select monthnos,(select top 1 Contribution from yourtbl where (select monthnos from tblmonth where actualmonno= MonthNum)<=monthnos inner join tblmonth on MonthNum=monthnos order by Monthnos desc ) as Contribution
from tblmonth
left join yourtbl on MonthNum=monthnos


results will be
monthnos   Contribution  
------------------------
1          900           
2          900           
3          900           
4          1000          
5          1000
6          1000
7          1000
8          1000
9          1000
10         1000
11         1000
12         852

Happy Coding!
:)
 
Share this answer
 
v3
Comments
ujjwal uniyal 26-Jul-12 7:40am    
it won't do the work as top 1 will only work here not in other cases. I need something general that should fill the table also it is a datatable not a table in database
Aarti Meswania 26-Jul-12 7:49am    
It is one kind of 'View' in sql, it is not a table. It is same output as you mentioned in your question. and I can't get you, is query showing error for 'top 1' keyword or you want something like sum of a group of month?

mention your table data and your desirable output table.
ujjwal uniyal 26-Jul-12 8:02am    
First table is dt_month which has all the months ,there srnum and there monthnum. srnum is 1 for april as it's the first month for financial year and it's month number is 4.
Second table is dt_gpfDets which has gpf details for the employee in that financial year. it has column gp_contribution, monthnum (corresponding to monthnum of tblmonths). what i need to do is fill in the rest of the months with data as i have mentioned in the question. i was thinking of using for each loop but how...
Aarti Meswania 26-Jul-12 8:47am    
see update solution you will get some hint from that
please correct query if some error you can follow logic from that.

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