Problem in converting Datatable to Ienumerable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cmrhema
    Contributor
    • Jan 2007
    • 375

    Problem in converting Datatable to Ienumerable

    Hi,

    I have a dataset that has two datatables.

    In the first datatable I have EmpNo,EmpName and EmpAddress

    In the second datatable I have Empno,EmpJoinda te, EmpSalary.

    I want a result where I should show EmpName as the label and his/her details in the gridview

    I populate a datalist with the first table, and have EmpNo as the datakeys.

    Then I populate the gridview inside the datatable which has EmpNo,EmpJoinDa te and EmpAddress.

    Code:
      DataTable dt = new DataTable();
            dt.Columns.Add("EmpNo");
            for (int i = 65; i < 70; i++)
            {
                DataRow dr = dt.NewRow();
                dr["EmpNo"] = i.ToString();
                dt.Rows.Add(dr);
            }
    
            DataTable dt2 = new DataTable();
            dt2.Columns.Add("EmpNo");
            dt2.Columns.Add("EmpName");
            for (int i = 65; i < 70; i++)
            {
                DataRow dr = dt2.NewRow();
                dr["EmpNo"] = i.ToString();
    
                dr["EmpName"] = Convert.ToChar(i);
                dt2.Rows.Add(dr);
            }
    
    
            Datalist1.DataSource = dt;
            Datalist1.DataBind();
            IEnumerable<DataRow> sequence = dt2.AsEnumerable();
    
            for (int i = 0; i < Datalist1.Items.Count; i++)
            {
                string EmpNo = Datalist1.DataKeys[i].ToString();
                string strExpr = "EmpNo =" + EmpNo.ToString();
                GridView Gridview1 = (GridView)Datalist1.Items[i].FindControl("Gridview1");
                Gridview1.DataSource = sequence.Where(t => t.Field<string>("EmpNo") == EmpNo);
                Gridview1.DataBind();
            }
    My Designer.aspx is as below

    Code:
     <asp:DataList ID="Datalist1" runat="server" DataKeyField="EmpNo">
                <ItemTemplate>
                    <asp:GridView ID="Gridview1" runat="server">
                    </asp:GridView>
                </ItemTemplate>
            </asp:DataList>
    On execution I do not observe any results, and I see errors as

    RowError HasErrors on the screen

    I included in the gridview the bound field control as

    Code:
    <asp:GridView ID="Gridview1" AutoGenerateColumns="true" runat="server">
                    <Columns>
                    <asp:BoundField DataField="EmpNo" />
                    </Columns>
                    </asp:GridView>
    And on execution, it throws an error stating A field or property with the name 'EmpNo' was not found on the selected data source.

    I checked the "sequence" using Debug mode, and it had values in the array, which I have highlighted,

    So where am I going wrong

    * Results View Expanding the Results View will enumerate the IEnumerable
    * [0] {System.Data.Da taRow} System.Data.Dat aRow HasErrors false bool
    * ItemArray {object[2]} object[] [0] "65" object {string} [1] "A" object {string} RowError "" string RowState Added System.Data.Dat aRowState
    * Table {} System.Data.Dat aTable
    * Static members
    * Non-Public members
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    In your case I would recommend that you look into using the DataSet.Relatio ns Property.

    This property is used to specify any relations between the tables within the DataSet. From there you should be able to use this to create a table that is the result of joining the 2 tables together on the Empno field in order to retrieve what you're looking for.

    For example, say you've already filled your FirstDataTable and your SecondDataTable and they are part of your DataSet (ds).

    You would first create the relationship between the two tables (VB code):
    Code:
    ds.Relations.Add("EmpNo", _
    ds.Tables["FirstDataTable"].Columns["EmpNo"], _
    ds.Tables["SecondDataTable"].Columns["EmpNo"], _
    true)
    Then to find the row where empNumber matches EmpNo in both tables:
    Code:
    DataRow row = ds.Tables["SecondTable"].Rows.Find(empNumber)

    -Frinny

    Comment

    • cmrhema
      Contributor
      • Jan 2007
      • 375

      #3
      Thanks Frinvale,
      I created a new datatable and gave
      dt=sequence.Cop yToDataTable<Da taRow>();

      the prblm was solved.
      Before the conversion, all the values were in array, so could not bind, and throwed error.

      Comment

      Working...