tags:

views:

104

answers:

3

Hello dear Stack-Overflow :3 I've got a kind of hard-case question.

So I'll try clearly explain my idea with my poor english :/ need select to DataTable SOME values from ???_**GetDisplayData **procedure for each day of previos month (GROUP IT) where group rules for different columns is different

I need to select some values in stored procedure from other stored procedure like this :

SELECT X FROM Y_Procedure(@ProcedureParameters)

Also I need to select from dynamic SQL Procedure like

Y_Procedure=@Y+'_Procedure'
SELECT X FROM Y_Procedure(@ProcedureParameters)

Also I need to load it to DataTable :-/

INSERT INTO @Report (CellHorizontal, CellVertical, CellValue) --to TABLE
SELECT Date,X2,X3 FROM Y_GetDisplayData(@Param)

    SET NOCOUNT OFF;

SELECT *
FROM @Report

GetDisplayData works as select with parameters and that doesn't returns SQL DataTable And there is no way to recode GetDisplayData's procedures, it's just constant procedures for me.

Finally I need to Group nodes from this table

    INSERT INTO @Report (CellHorizontal, CellVertical, CellValue) 
    SELECT T1.Date, 
IF ((Select grouptype...)=1) T1.X2 + T2.X2
    ELSE IF ((Select grouptype...)=2) AVG(T1.X2,T2.X2), 
IF ((Select grouptype...)=1) T1.X3 + T2.X3
    ELSE IF ((Select grouptype...)=2) AVG(T1.X3,T2.X3), 
    (SELECT T2.Date,X2,X3 FROM Y_GetDisplayData(@Param) T2
    WHERE T2.Date>T1.Date AND T2.Date>=T1.Date)
    FROM Y_GetDisplayData(@Param) T1
    GROUP BY EVERY DAY ???
    --and here is epic fail

I can make all stuff on asp server :

And C# allows me to use something like SelectCommand = IzmProc + "_GetDisplayData";

And then I will work (select special data) with DataTables on ASP.NET Server but it's better to make all on SQL ... But looking like it's just unrealizable on SQL >_<

I gonna think about C# realization

but my code is very weird , got errors and doesn't works >_<

    public static DataTable GetReport(string Param)
    {
        System.Configuration.ConnectionStringSettings connSetting = ConfigurationManager.ConnectionStrings["FlowServerConnectionString"];
        SqlConnection conn = new SqlConnection(connSetting.ConnectionString);
        SqlCommand cmd = new SqlCommand(Param + " _GetDisplayData 90,1,1,80,1,1,0");
        try
        {
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);

            //------------------------------------------------------------
            int dayOfWeekNumber = (int)DateTime.Today.DayOfWeek - (int)System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;
            var previosWeekFirstDay = DateTime.Today.AddDays(-7 - dayOfWeekNumber);
            var yearsterday = DateTime.Today.AddDays(-1);
            var previosWeekLastDay = previosWeekFirstDay.AddDays(6);
            var previosMonthFirstDay = DateTime.Today.AddMonths(-1);
            previosMonthFirstDay = previosMonthFirstDay.AddDays(-previosMonthFirstDay.Day + 1);
            var previosMonthLastDay = previosMonthFirstDay.AddDays(DateTime.DaysInMonth(previosMonthFirstDay.Year, previosMonthFirstDay.Month) - 1);
            //-------------------------------------------------------------
            //Ok... now I need to group it on DT->everyday in month
            DataRow[] drx;
            DataTable newDT= new DataTable();
            foreach (DataRow row in dt.Rows)
            {
                string s = row["DailyRecTime"].ToString();
                drx = dt.Select("DailyRecTime= '" + s + "'");
                for (DateTime x = previosMonthFirstDay; x <= previosMonthLastDay; x.AddDays(1))
                {
                    foreach(DataRow drr in drx)
                    if (x.ToString() == drr[0].ToString())
                    {
                        drx = dt.Select("DailyRecTime= '" + s + "'");
                        DataRow newRow = new DataRow(); //ERROR HERE
                        double[] temp = new double[drx[0].Table.Columns.Count];
                        foreach (DataRow dr in drx)
                        {
                            for(int dc=1; dc<dr.Table.Columns.Count; dc++)
                            {
                                if (dr.Table.Columns[dc].Caption.ToString() == "C1_mol") //for example
                                    temp[dc] += double.Parse(dr[dc].ToString()); // nonsense
                            }
                        }
                        foreach (DataColumn dcl in drx[0].Table.Columns)
                        {
                            newRow.Table.Columns.Add(dcl);
                            newRow[dcl] = temp[dcl]; //error here :)
                        }
                        newDT.Rows.Add(newRow);
                    }
                }
            }

            return newDT;
        }
        catch (Exception)
        {
            return null;
        }

omg ... :-/

and thank you for reading and trying to help me :3

+1  A: 
 DataRow newRow = new DataRow(); //ERROR HERE 

try

DataRow newRow = dt.NewRow();
Ballin
ok :) how to transfer row[colindex] value , like I want here : newRow[dcl] = temp[dcl];
nCdy
+1  A: 
DataRow[] foundRows = dt.Select(null,dt.Columns[0].ColumnName); // Sort with Column name
for (int i = 0 ; i <= foundRows.GetUpperBound(0); i++) 
{ object[] arr = new object[foundRows.GetUpperBound(0)];
for (int j = 0; j <= foundRows[i].ItemArray.GetUpperBound(0); j++)
{ arr[j]=foundRows[i][j]; }
DataRow data_row = newDT.NewRow();
data_row.ItemArray=arr;
newDT.Rows.Add(data_row);

}

got this from
//Hussain Hyder Ali Khowaja // Karachi, Pakistan

Ballin
can you make a comments how it works :S I can't understand how I can use it with my task yet :(
nCdy
A: 

ohk rather make a data view like this, and use te sort function it has

dataview v =dt.defaultview;
v.sort="columnName DESC";//columName can be DailyRecTime, DESC fro decending or ASC for asscending
dt=v.toTable();
Ballin