Click here to Skip to main content
15,906,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
sql table contains
ID TestCase ExecutionTime
1 Test1 5
2 Test1 10
3 Test2 5
4 Test3 6


from sql table i want to show ExecutionTime in grid as TotalHours TestCase Should not be repeate and want to calculate and show like example Test1 Total Hours 15..
hi how to calculate total execution hours in gridview for example:
-------------GridView----------
ID  TestCase    TotalHours
1    Test1    
2    Test2
3    Test3

can any one suggest me how to do this?
thanks!!
Posted
Comments
ythisbug 21-Feb-12 4:23am    
and i want to show all records from tat table in grid..

SQL
SELECT LID, BuildNo, TID, Status, StarDate, EndDate, Sum(ExecutionTime)
FROM tblTestersWorkStatus
Group BY LID, BuildNo, TID, Status, StarDate, EndDate

Fetch The Output of this query into Datatable and then bind the GridView Datasource with this datatable.
 
Share this answer
 
Comments
ythisbug 21-Feb-12 7:31am    
before i tried same query..if i give like this query m getting error like..

System.Web.HttpException: DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'ExecutionTime'. Source Error: Line 56: <asp:TemplateField HeaderText="ExecutionTime" ItemStyle-Width="150"> Line 57: Line 58: <%# DataBinder.Eval(Container.DataItem,"ExecutionTime") %> Line 59:
Aniket Yadav 21-Feb-12 7:43am    
SELECT LID, BuildNo, TID, Status, StarDate, EndDate, Sum(ExecutionTime) AS ExecutionTime
FROM tblTestersWorkStatus
Group BY LID, BuildNo, TID, Status, StarDate, EndDate

Try This
ythisbug 21-Feb-12 7:51am    
but repeating TID i used DISTINCT also in ur query..
ythisbug 21-Feb-12 7:48am    
thanks brother..
Aniket Yadav 21-Feb-12 8:28am    
You are always welcome. It was my pleasure to help you.
SQL
sample test use it will work for you :)

CREATE TABLE #test(id int,testcase varchar (10),ExecutionTime int)
INSERT INTO #test VALUES(1,'test1',2)
INSERT INTO #test VALUES(2,'test1',2)
select Row_number() OVER(order by TestCase) AS id, TestCase, SUM(ExecutionTime)
FROM #test
GROUP BY TestCase
drop table #test
 
Share this answer
 
Comments
ythisbug 21-Feb-12 4:21am    
i created table before only..and i created sp but its coming error..
Sudip Saha 21-Feb-12 4:48am    
i have used as temp table use ur table and remove drop table #test from the statement use only select Row_number() OVER(order by TestCase) AS id, TestCase, SUM(ExecutionTime) FROM [your table name ]GROUP BY TestCase
in our SP
ythisbug 21-Feb-12 4:52am    
i created but error coming like Msg 156, Level 15, State 1, Procedure usp_TotalExecutionTime, Line 4
Incorrect syntax near the keyword 'BEGIN'.
On your query you have to use DateDiff() function to determine the excutiontime between to date values.

see here[^].
 
Share this answer
 
v2
Comments
ythisbug 21-Feb-12 4:22am    
hi thanks but not this type of example..it will help me for future..thanks
you can fetch records group by TestCase column

select ID, TestCase, count(ExecutionTime)
from tablename
group by TestCase,ID
 
Share this answer
 
v2
Comments
ythisbug 21-Feb-12 4:34am    
is this storedprocedure
kashif Atiq 21-Feb-12 4:44am    
this is a query you can write it in stored procedure or if you are writing query in your code as string than use it in inline code also if you want it to sorted by testcase than write
select ID, TestCase, count(ExecutionTime)
from tablename
group by TestCase,ID
order by TestCase
Hi, use below query

SQL
select Row_number() OVER(order by (select 0)) AS id, TestCase, SUM(ExecutionTime) FROM TableName GROUP BY TestCase



Don't forget to mark if it solve your problem. :-)
 
Share this answer
 
Comments
ythisbug 21-Feb-12 4:46am    
hi m getting error near BEGIN
ythisbug 21-Feb-12 4:47am    
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE usp_TotalExecutionTime


BEGIN

SET NOCOUNT ON;


SELECT Row_number() OVER (Order BY(select 0))LID,BuildNo,TID,Status,StarDate,EndDate,Sum(ExecutionTime)
from tblTestersWorkStatus
Group BY TID
END
GO


//this my stored procedure..m getting error like
Msg 156, Level 15, State 1, Procedure usp_TotalExecutionTime, Line 4
Incorrect syntax near the keyword 'BEGIN'.
kashif Atiq 21-Feb-12 4:50am    
remove SET NOCOUNT ON; line after begin
ythisbug 21-Feb-12 4:50am    
i removed but also same error..
kashif Atiq 21-Feb-12 4:56am    
write [as] before begin

it should be
...
.
CREATE PROCEDURE usp_TotalExecutionTime

as
BEGIN
...
end
No need of any stored procedure for such a simple query

Fire the below query

SQL
SELECT TestCase, SUM(ExecutionTime) FROM YourTableName GROUP BY TestCase


This query will give you distinct records with total execution time for particular testcase.

Enjoy...
 
Share this answer
 
Comments
ythisbug 21-Feb-12 5:29am    
<asp:GridView ID="grdTest" runat="server" Style="z-index: 118;" Width="433px" Height="114px"
AutoGenerateColumns="false"
OnRowDataBound="grdTest_RowDataBound" AllowPaging="True"
onpageindexchanging="grdTest_PageIndexChanging" PageSize="5">
<columns>
<asp:TemplateField HeaderText="Tester" ItemStyle-Width="150">
<itemtemplate>
<%# DataBinder.Eval(Container.DataItem,"LID") %>


<itemstyle width="150px">

<asp:TemplateField HeaderText="BuildNo" ItemStyle-Width="150">
<itemtemplate>
<%# DataBinder.Eval(Container.DataItem,"BuildNo") %>


<itemstyle width="150px">

<asp:TemplateField HeaderText="TestCase" ItemStyle-Width="150">
<itemtemplate>
<%# DataBinder.Eval(Container.DataItem,"TID") %>


<itemstyle width="150px">

<asp:TemplateField HeaderText="Status" ItemStyle-Width="150">
<itemtemplate>
<%# DataBinder.Eval(Container.DataItem,"Status") %>


<itemstyle width="150px">


<asp:TemplateField HeaderText="Start Date" ItemStyle-Width="150">
<itemtemplate>
<%# DataBinder.Eval(Container.DataItem,"StarDate") %>


<itemstyle width="150px">


<asp:TemplateField HeaderText="End Date" ItemStyle-Width="150">
<itemtemplate>
<%# DataBinder.Eval(Container.DataItem,"EndDate") %>


<itemstyle width="150px">


<asp:TemplateField HeaderText="ExecutionTime" ItemStyle-Width="150" FooterText="Sum">
<itemtemplate>
<%# DataBinder.Eval(Container.DataItem, "ExecutionTime")%>

<footertemplate>
<asp:Label ID="Sum" runat="server">


<itemstyle width="150px">





<HeaderStyle CssClass="style3"></HeaderStyle>



this contains in my aspx page..when i gave query like

"cmd.CommandText = "SELECT TID, SUM(ExecutionTime) FROM tblTestersWorkStatus GROUP BY TID";"

so m getting error like

System.Web.HttpException: DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'LID'.

Source Error:


Line 56: <asp:TemplateField HeaderText="Tester" ItemStyle-Width="150">
Line 57: <itemtemplate>
Line 58: <%# DataBinder.Eval(Container.DataItem,"LID") %>
Line 59:
Line 60:
Aniket Yadav 21-Feb-12 5:36am    
Infact You Have Made A Spelling mistake while binding asp:TemplateField HeaderText="Tester". Please check that while defining in datagrid, you write the correct field name same as database field name.
ythisbug 21-Feb-12 5:49am    
ya..but i gave example..but m giving correct filed name same as database field name
Aniket Yadav 21-Feb-12 5:54am    
Actually This Is Not The Error from the query, rather it is error from your code. The error has specified that 'System.Data.DataRowView' does not contain a property with the name 'LID'.
Please see to that.

Have you executed the Query in SQL Server or the database you are using... If no then please check it.
ythisbug 21-Feb-12 6:20am    
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE usp_TotalExecutionTime

AS

BEGIN




SELECT Row_number() OVER (Order BY TID)LID,BuildNo,TID,Status,StarDate,EndDate,Sum(ExecutionTime)
from tblTestersWorkStatus
Group BY TID,LID,BuildNo,TID,Status,StarDate,EndDate
END
GO
this is my query in sql as sp..
Hi ythisbug

Your query to return the ExecutionTime for each test case is:

SQL
select executiontime from TABLE_NAME where testcase = 'Test1'


You can then use the SqlDataReader Class to get the results of this query and add them together, something like:

C#
string queryString ="select executiontime from TABLE_NAME where testcase = ";
string getTestCases = "select distinct TestCase from TABLE_NAME;";
 using (SqlConnection connection = new SqlConnection(connectionString))
 {
     int iTotalTime = 0;
     SqlCommand command =
         new SqlCommand(getTestCases, connection);
     connection.Open();
     SqlDataReader reader = command.ExecuteReader();
     // Call Read before accessing data.
     while (reader.Read())
     {
        // Build the execution time query
        queryString = queryString + reader[0] + ";";
        command.CommandText = queryString; 
        SqlDataReader timereader = command.ExecuteReader();
        while(timereader.Read())
        {
           iTotalTime = iTotalTime + (int)timereader[0];  
        }
        // You now have the TestCase and TotalExecution Time
        // You should be able to set up a datasource for your grid
        // using a collection of some type... Let me know if you need
        // me to complete....
     }
 }


The question is how are you populating the grid? Are you binding directly to an SQL query? Or are you building up the grid yourself?
So you could select distinct from the Table to get a distinct list of TestCases and then use those in a loop to get the totalExecution times for each test case. Then you can create a list of testcases and execution times and use that list to populate the grid.

What I suggest is to use a DataSource to populate your grid. So create an object that will contain the data you are interested in eg:

C#
class TestResults
{
    public String Test_Name { get; set; }
    public int Test_Execution_Time { get; set; }
}


Then you can use this to hold the results from the query.
C#
list<TestResults> testResultsList = new List<TestResults>();
TestResults tRes = new TestResult();
tRes.Test_Name = reader[0];
tRes.Test_Execution_Time = iTotalTime;
// Add to a list of TestResults 
testResultsList.Add(tRes); // Perform this for every Test

// Use this list to populate the Grid.
dataGridView1.DataSource = testResultsList;
 
Share this answer
 
v3
Comments
ythisbug 21-Feb-12 4:27am    
ok think m giving distinc for testcase..in sql there two records of test1 so i want to show total time and want to calculate and show in grid as total
ythisbug 21-Feb-12 4:36am    
m buildin up the grid my self..
ythisbug 21-Feb-12 4:54am    
m not using datasource bro.
Aniket Yadav 21-Feb-12 5:30am    
Have you tried my solution?
ythisbug 21-Feb-12 6:38am    
can u suggest me where to place last code??is it all in same page??

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