Connect with Expertise | Find Experts, Get Answers, Share Insights

Problem in converting Datatable to Ienumerable

C
 
Join Date: Jan 2007
Posts: 374
#1: Mar 14 '10
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,EmpJoindate, 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,EmpJoinDate and EmpAddress.

Expand|Select|Wrap|Line Numbers
  1.   DataTable dt = new DataTable();
  2.         dt.Columns.Add("EmpNo");
  3.         for (int i = 65; i < 70; i++)
  4.         {
  5.             DataRow dr = dt.NewRow();
  6.             dr["EmpNo"] = i.ToString();
  7.             dt.Rows.Add(dr);
  8.         }
  9.  
  10.         DataTable dt2 = new DataTable();
  11.         dt2.Columns.Add("EmpNo");
  12.         dt2.Columns.Add("EmpName");
  13.         for (int i = 65; i < 70; i++)
  14.         {
  15.             DataRow dr = dt2.NewRow();
  16.             dr["EmpNo"] = i.ToString();
  17.  
  18.             dr["EmpName"] = Convert.ToChar(i);
  19.             dt2.Rows.Add(dr);
  20.         }
  21.  
  22.  
  23.         Datalist1.DataSource = dt;
  24.         Datalist1.DataBind();
  25.         IEnumerable<DataRow> sequence = dt2.AsEnumerable();
  26.  
  27.         for (int i = 0; i < Datalist1.Items.Count; i++)
  28.         {
  29.             string EmpNo = Datalist1.DataKeys[i].ToString();
  30.             string strExpr = "EmpNo =" + EmpNo.ToString();
  31.             GridView Gridview1 = (GridView)Datalist1.Items[i].FindControl("Gridview1");
  32.             Gridview1.DataSource = sequence.Where(t => t.Field<string>("EmpNo") == EmpNo);
  33.             Gridview1.DataBind();
  34.         }
My Designer.aspx is as below

Expand|Select|Wrap|Line Numbers
  1.  <asp:DataList ID="Datalist1" runat="server" DataKeyField="EmpNo">
  2.             <ItemTemplate>
  3.                 <asp:GridView ID="Gridview1" runat="server">
  4.                 </asp:GridView>
  5.             </ItemTemplate>
  6.         </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

Expand|Select|Wrap|Line Numbers
  1. <asp:GridView ID="Gridview1" AutoGenerateColumns="true" runat="server">
  2.                 <Columns>
  3.                 <asp:BoundField DataField="EmpNo" />
  4.                 </Columns>
  5.                 </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.DataRow} System.Data.DataRow HasErrors false bool
* ItemArray {object[2]} object[] [0] "65" object {string} [1] "A" object {string} RowError "" string RowState Added System.Data.DataRowState
* Table {} System.Data.DataTable
* Static members
* Non-Public members

✓ answered by cmrhema

Thanks Frinvale,
I created a new datatable and gave
dt=sequence.CopyToDataTable<DataRow>();

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

Frinavale's Avatar
E
M
C
 
Join Date: Oct 2006
Location: The Great White North
Posts: 7,106
#2: Mar 17 '10

re: Problem in converting Datatable to Ienumerable


In your case I would recommend that you look into using the DataSet.Relations 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):
Expand|Select|Wrap|Line Numbers
  1. ds.Relations.Add("EmpNo", _
  2. ds.Tables["FirstDataTable"].Columns["EmpNo"], _
  3. ds.Tables["SecondDataTable"].Columns["EmpNo"], _
  4. true)
Then to find the row where empNumber matches EmpNo in both tables:
Expand|Select|Wrap|Line Numbers
  1. DataRow row = ds.Tables["SecondTable"].Rows.Find(empNumber)

-Frinny
C
 
Join Date: Jan 2007
Posts: 374
#3: Mar 18 '10

re: Problem in converting Datatable to Ienumerable


Thanks Frinvale,
I created a new datatable and gave
dt=sequence.CopyToDataTable<DataRow>();

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

Tags
ienumerable datatable