Dharmendra Kumar Singh
Dharmendra Kumar Singh

Reputation: 3001

Fix- DataBinding: 'System.Data.DataRowView' does not contain a property with the name

create procedure St_Proc_GetUserReportforCurrentDayTask                  
@userID int                  
as                  
    Begin                  
        set NoCount on;                  
        DECLARE @TODAY DATE                    
        SET @TODAY = CONVERT(VARCHAR(10), GETDATE(), 111)                  
        select  CONVERT(VARCHAR,production.CalendarDate,101) + RIGHT (CONVERT(VARCHAR,production.CalendarDate , 100 ) ,7) as Date,                   
        RegionAndProjectInfo.RegionProjectName as Region ,                  
        County.CountyName as County,                  
        WorkType.WorkTypeName as WorkType,                  
        Task.TaskName as Task,      
        Production.VolumeProcessed as 'Volumes Processed',                  
        Production.TimeSpent as 'Duration(HH:MM)'                  
        from Production                   
        inner join RegionAndProjectInfo                  
        on                  
        RegionAndProjectInfo.RegionProjectID=Production.RegionProjectID                  
        inner join County                  
        on                   
        County.CountyID=Production.CountyID                  
        inner join WorkType                  
        on                  
        WorkType.WorkTypeID=Production.WorkTypeID                  
        inner join Task                  
        on                  
        Task.TaskID=Production.TaskID                  
        where Production.UserID=@userID and CalendarDate >= @TODAY                  
    End 

This is my stored procedure and i am executing this stored procedure and saving the result in a dataset and then binding a grid with this dataset.

private void BindGridOnPageLoad()
    {
        _production = new EmployeeQuotientCL.Production();
        _userEcode = Convert.ToString(Session["UserID"]);
        int _userID = _production.GetUserIDFromDB(_userEcode);
        dsUserTaskDetails = _production.GetTabularUsertaskDetails(_userID);
        BindGridView(dsUserTaskDetails);
    }

Up to here every thing is fine .Then i got requirement to show Duration(HH:MM) total in the footer of the grid view.I had added following code in the page:-

<div id="gridview">
            <asp:GridView ID="GVUserEnteredDetails" runat="server" ShowFooter="true" CssClass="mGrid"  CellSpacing="2"   onselectedindexchanged="GVUserEnteredDetails_SelectedIndexChanged" onrowdatabound="GridView1_RowDataBound">                  
            </asp:GridView>

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            tp += Convert.ToDecimal(((DataRowView)e.Row.DataItem).Row["[Duration(HH:MM)]"]);
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[0].Text = "Total Hours : ";
            e.Row.Cells[1].Text = tp.ToString("c");
            e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Font.Bold = true;

        }
    }

But when i am debugging i m getting this error:- "DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Duration'." What the mistake i m doing and how to fix it? Any suggestion and help will be helpful for me.

Stack Trace:-

[ArgumentException: Column '[Duration(HH:MM)]' does not belong to table Table.] System.Data.DataRow.GetDataColumn(String columnName) +2124943 System.Data.DataRow.get_Item(String columnName) +13 EQ.Associates.Production.GridView1_RowDataBound(Object sender, GridViewRowEventArgs e) in C:\Users\dsingh\Documents\Visual Studio 2010\Projects\EQ\EQ\Associates\Production.aspx.cs:112 System.Web.UI.WebControls.GridView.OnRowDataBound(GridViewRowEventArgs e) +115 System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) +181 System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +3896 System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +66 System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) +14 System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +128 System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +33 System.Web.UI.WebControls.DataBoundControl.PerformSelect() +143 System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +74 System.Web.UI.WebControls.GridView.DataBind() +4 EQ.Associates.Production.BindGridView(DataSet ds) in C:\Users\dsingh\Documents\Visual Studio 2010\Projects\EQ\EQ\Associates\Production.aspx.cs:603 EQ.Associates.Production.BindGridOnPageLoad() in C:\Users\dsingh\Documents\Visual Studio 2010\Projects\EQ\EQ\Associates\Production.aspx.cs:593 EQ.Associates.Production.Page_Load(Object sender, EventArgs e) in C:\Users\dsingh\Documents\Visual Studio 2010\Projects\EQ\EQ\Associates\Production.aspx.cs:78 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35 System.Web.UI.Control.OnLoad(EventArgs e) +91 System.Web.UI.Control.LoadRecursive() +74 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207

Upvotes: 1

Views: 7579

Answers (2)

V4Vendetta
V4Vendetta

Reputation: 38230

As you mentioned in the comments you are facing trouble in the Decimal parse of the data you have represented as HH:mm

you should be using a TimeSpan to sum all these values instead of a decimal. (TimeSpan has parse exact functionality from 4.0 but if you are on a lesser version)

Try doing it in this fashion

TimeSpan ts = DateTime.ParseExact("02:30", "HH:mm", CultureInfo.InvariantCulture).TimeOfDay;
// instead of 02:30 you would have the string from your column passed in

// you can add up the values in this fashion
ts.Add(DateTime.ParseExact("08:30", "HH:mm", CultureInfo.InvariantCulture).TimeOfDay);

The Resultant Timespan you can use to display it the format you choose as it would also have number of days too or you can always show ts.TotalMinutes

Hope this is what you were looking out for else please update for the relevant part

Upvotes: 1

Rumit Parakhiya
Rumit Parakhiya

Reputation: 2714

Try using sqare bracket around your column name as it is containing special characters. like,

 tp += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "[Duration(HH:MM)]"));

Upvotes: 0

Related Questions