Data loss between SQL Server and Datatable column

203 views Asked by At

I have a stored procedure that calculates employees time based on job tile and breaks it out by regular/overtime, Total hours, cost, and percentages.

I am loading the resulting table into a datatable on a webpage (C#.NET) to use as a datasource for a grid. The code for SQL is:

ELSE IF @type = 'Reports_MH_WO_JobTitles'
BEGIN
    DECLARE @TS_JT_R_O TABLE ([JobTitle] NVARCHAR(100),[R] DECIMAL(5,1),[O] DECIMAL(5,1), [Bill Rate] DECIMAL(18,2))

    -- Code Here to populate @TS_JT_R_O

    SELECT row_number() OVER (ORDER BY [JobTitle]) [Sort]
    ,T.[JobTitle]
    ,SUM([R]) [R]
    ,SUM([O]) [O]
    ,SUM([R]+[O]) [TOTAL]
    ,CAST(SUM([Cost]) AS DECIMAL(18,2)) [Cost]
    ,CAST(SUM([R]+[O])/( SELECT SUM([R]+[O]) FROM @TS_JT_R_O ) AS DECIMAL(6,5)) [HoursPer]
    ,CAST(SUM([Cost])/( SELECT SUM([COST]) FROM ( SELECT (SUM([R])*[Bill Rate])+(SUM([O])*[Bill Rate]*1.5) [COST] FROM @TS_JT_R_O GROUP BY [Bill Rate] ) Z ) AS DECIMAL(6,5)) [CostPer]
    FROM 
        ( SELECT [JobTitle],SUM([R]) [R],SUM([O]) [O],SUM([R]+[O]) [TOTAL], (SUM([R])*[Bill Rate])+(SUM([O])*[Bill Rate]*1.5) [Cost] FROM @TS_JT_R_O GROUP BY [JobTitle], [Bill Rate] ) T
    GROUP BY T.[JobTitle]
    UNION
    SELECT '999999999'[Sort], 'GRAND TOTAL' [JobTitle], SUM([R]) [R], SUM([O]) [O], SUM([R]+[O]) [TOTAL], CAST(SUM([Cost]) AS DECIMAL(18,2)) [Cost],1,1
    FROM 
    ( SELECT [JobTitle],SUM([R]) [R],SUM([O]) [O],SUM([R]+[O]) [TOTAL], (SUM([R])*[Bill Rate])+(SUM([O])*[Bill Rate]*1.5) [Cost] FROM @TS_JT_R_O GROUP BY [JobTitle], [Bill Rate] ) T
    ORDER BY [Sort],[JobTitle]

END

The code behind for populating the grid:

    sqlStr = "WorkOrder_SelectStatements";
    cmd = new SqlCommand(sqlStr, cnn);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandTimeout = 0;
    cmd.Parameters.Add("@type", SqlDbType.NVarChar).Value = "Reports_MH_WO_JobTitles";
    cmd.Parameters.Add("@ID", SqlDbType.NVarChar).Value = ddlWorkOrder.SelectedValue;
    if (rdpStart.SelectedDate != null)
        cmd.Parameters.Add("@startDate", SqlDbType.NVarChar).Value = txtStart.Text;
    if (rdpEnd.SelectedDate != null)
        cmd.Parameters.Add("@endDate", SqlDbType.NVarChar).Value = txtEnd.Text;
    da = new SqlDataAdapter(cmd);
    da.SelectCommand.CommandTimeout = 0;
    DataTable dt = new DataTable();
    da.Fill(dt);
    ds.Tables.Add(dt);
    grid.DataSource = dt;
    grid.DataBind();

While debugging the DataAdapter fills the DataTable but the columns for the "Cost" and the "CostPer" from SQL go into the DataTable as NULL. Out of the missing data the only cell that populates is the 100% for the cost percentage for the UNION SELECT.

I am at a loss as to where only some of the data is dropped. Any ideas where this data loss between SQL and the DataAdapter is coming from?

Screenshots:

SQL

SQL Data Result Set

Visual Studio Debug View

Debug view from VS

Web Page Resulting View

Web Site

0

There are 0 answers