How to get data in gridview from one table but multiple select statements

feby

In asp.net c# I am trying to return the total sum when the flag = 'D' as sumDebt and the total sum when the flag = 'C' as sumcredit my code is :

    SqlParameter[] para = new SqlParameter[4];
    para[0] = new SqlParameter("@stat_leger", ddlACCcode.SelectedValue);
    para[1] = new SqlParameter("@branch", DDLBranch.SelectedValue);
    para[2] = new SqlParameter("@from", db.getDate(txtFrom.Text));
    para[3] = new SqlParameter("@to", db.getDate(txtTo.Text));

    DataTable dtreport = db.SelectCmdText("Select sum(stat_amount) as sumDebt from PostedVoucher where stat_flag ='D' and stat_leger =@stat_leger and branch=@branch and stat_date between @from and @to UNION Select sum(stat_amount) as sumcredit from PostedVoucher where stat_flag ='C' and stat_leger =@stat_leger and branch=@branch  and stat_date between @from and @to ", para);

    GridView1.DataSource = dtreport;
    GridView1.DataBind();

in html the grid view :

  <asp:TemplateField HeaderText="sumDebt">
            <ItemTemplate>
                 <asp:Label ID="sumDebt" runat="server" Text='<%#string.Format("{0:0.000}",float.Parse(Eval("sumDebt").ToString()))%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

            <asp:TemplateField HeaderText="sumcredit">
            <ItemTemplate>
                 <asp:Label ID="sumcredit" runat="server" Text='<%#string.Format("{0:0.000}",float.Parse(Eval("sumcredit").ToString()))%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

the problem that in gridview it's return an error ( DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'sumcredit'.)

Sharique Ansari

To merge Table try like this:-

    SqlConnection conn = new SqlConnection("connectionString");
    SqlCommand cmd = new SqlCommand("Select sum(stat_amount) as sumDebt from PostedVoucher where stat_flag ='D' and stat_leger =@stat_leger and branch=@branch and stat_date between @from and @to UNION Select sum(stat_amount) as sumcredit from PostedVoucher where stat_flag ='C' and stat_leger =@stat_leger and branch=@branch  and stat_date between @from and @to", conn);
    cmd.Parameters.AddWithValue("@stat_leger", ddlACCcode.SelectedValue);
    cmd.Parameters.AddWithValue("@branch", DDLBranch.SelectedValue);
    cmd.Parameters.AddWithValue("@from", db.getDate(txtFrom.Text));
    cmd.Parameters.AddWithValue("@to", db.getDate(txtTo.Text));
    conn.Open();
    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
    DataSet dataSet = new DataSet();
    adapter.Fill(dataSet);       


    DataTable tbl1 = dataSet.Tables[0];
    DataTable tbl2 = dataSet.Tables[1];
    tbl1.Columns.Add("sumcredit");

    for (int i = 0; i < tbl2.Rows.Count; i++)
    {
        DataRow dr = tbl2.Rows[i];
        DataRow dr1 = tbl1.Rows[i];
        foreach (DataColumn v_Column in dr.Table.Columns)
        {
            if (dr1.Table.Columns.Contains("sumcredit"))
            {
                dr1["sumcredit"] = dr["sumcredit"];
            }
        }
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to include multiple select statements in one table

From Dev

How to read data from sp which is having multiple select statements?

From Dev

How to insert multiple select statements into a temp table

From Dev

how to get multiple data from two different table from one database in php

From Dev

how to get data from a gridview

From Dev

Combining Multiple Select Statements Into One With A Count from Each Select Statement

From Dev

Select data from multiple table

From Dev

SQL: How to select multiple values from one table as seperate columns

From Dev

how to select one row from one table and multiple rows from other table using joins in mysql,

From Dev

How to get one result with multiple data in other table

From Dev

Inserting one MySQL row with values from multiple, nested SELECT statements

From Dev

How to return result of many select statements as one custom table

From Dev

How to append data from one GridView to second GridView?

From Dev

Select multiple columns from a table, but group by one

From Dev

PHP SQL - Multiple select from one table

From Dev

How does one populate a structure using multiple select statements

From Dev

How to select data from table to get output in transpose or crosstab manner?

From Dev

How to get data from related table in Laravel (one to many)?

From Dev

How to get cell data from gridview?

From Dev

how to get textbox data from GridView?

From Dev

How to get data from TextBox in gridview?

From Dev

How can I get data for one field from multiple tables?

From Dev

How to select rows from one data.table to apply in another data.table?

From Dev

how to multiple select data with sort data to new column (like AS Column) in one table..?

From Dev

Select Data from multiple table and Group by Category

From Dev

CakePHP 3 : select data from multiple table

From Dev

how to select latest record from table by using Group by statements

From Dev

how to select latest record from table by using Group by statements

From Dev

Select from multiple table and left join to one table

Related Related

  1. 1

    How to include multiple select statements in one table

  2. 2

    How to read data from sp which is having multiple select statements?

  3. 3

    How to insert multiple select statements into a temp table

  4. 4

    how to get multiple data from two different table from one database in php

  5. 5

    how to get data from a gridview

  6. 6

    Combining Multiple Select Statements Into One With A Count from Each Select Statement

  7. 7

    Select data from multiple table

  8. 8

    SQL: How to select multiple values from one table as seperate columns

  9. 9

    how to select one row from one table and multiple rows from other table using joins in mysql,

  10. 10

    How to get one result with multiple data in other table

  11. 11

    Inserting one MySQL row with values from multiple, nested SELECT statements

  12. 12

    How to return result of many select statements as one custom table

  13. 13

    How to append data from one GridView to second GridView?

  14. 14

    Select multiple columns from a table, but group by one

  15. 15

    PHP SQL - Multiple select from one table

  16. 16

    How does one populate a structure using multiple select statements

  17. 17

    How to select data from table to get output in transpose or crosstab manner?

  18. 18

    How to get data from related table in Laravel (one to many)?

  19. 19

    How to get cell data from gridview?

  20. 20

    how to get textbox data from GridView?

  21. 21

    How to get data from TextBox in gridview?

  22. 22

    How can I get data for one field from multiple tables?

  23. 23

    How to select rows from one data.table to apply in another data.table?

  24. 24

    how to multiple select data with sort data to new column (like AS Column) in one table..?

  25. 25

    Select Data from multiple table and Group by Category

  26. 26

    CakePHP 3 : select data from multiple table

  27. 27

    how to select latest record from table by using Group by statements

  28. 28

    how to select latest record from table by using Group by statements

  29. 29

    Select from multiple table and left join to one table

HotTag

Archive