i am using DataRelation to join two datatables. here is the code.
Dim dt1 As New DataTable
dt1.Load(dr1)
Dim dt2 As New DataTable
dt2.Load(dr2)
Dim datarelation As DataRelation
Dim dc1 As DataColumn
Dim dc2 As DataColumn
Dim ds As New DataSet
ds.Tables.Add(dt1)
ds.Tables.Add(dt2)
dc1 = dt1.Columns("CL_R")
dc2 = dt2.Columns("CL_R")
datarelation = New DataRelation("JoinData", dc1, dc2)
ds.Relations.Add(datarelation)
NewDg.DataSource = ds
NewDg.DataBind()
I would like to display both the tables, dt1 and dt2 in the datagrid, NewDg.
Now it dispalys only the data of datatable dt1 as this is parent table.
I want to dispaly both the parent table and child table. please help me. thank you
hi, thank you for your reply. But that did not help me much. I have finished pretty much what was in that article.
I have issue getting the child table contents and parent table contents into one table.
Could you please let me know if there is a way to join two data tables.
I tried using LINQ but I am getting error.
Dim dt1 As New DataTable
dt1.Load(dr1)
Dim dt2 As New DataTable
dt2.Load(dr2)
Dim joinquery = From t1 In dt1.AsEnumerable(), t2 In dt2.AsEnumerable() _
Where (t1.Field(Of Long)("CL_R") = _
t2.Field(Of Long)("CL_R")) _
Select t1, t2
NewDg.DataSource = joinquery
NewDg.DataBind()
When I debug this, I get the error, System.InvalidCastException: Specified cast is not valid. I get this error in the line, joinquery =... I also have used Integer as the field type for the column, CLASS_NO.
Dim joinquery = From t1 In dt1.AsEnumerable(), t2 In dt2.AsEnumerable() _ Where (t1.Field(Of Long)("CL_R") = _ t2.Field(Of Long)("CL_R")) _ Select t1, t2 NewDg.DataSource = joinquery NewDg.DataBind()
Hello, you can change to this——
Dim joinquery =From t1 In dt1.AsEnumerable(),_
From t2 in dt2.AsEnumerable(), _
Where t1["FieldName"].ToString()==t2["FieldName"].ToString()
Select New With
{
Key .NewField = t1["FieldName1"],
…………………………
System.Web.HttpException: The data source for GridView with id '' did not have any properties or attributes from which
to generate columns. Ensure that your data source has content.
I checked for the content in joinquery and it has the rows of the two tables, t1 and t2. So I am not sure why it is not binding the data.
The appilcation was working fine till yesterday, now it gives me the error,I am not sure why. I did not change anything. I left the code as it is on weekend and today after coming back, it gives error. could you please help me. thank you.
Unable to cast object of type '<JoinIterator>d__61`4[System.Data.DataRow,System.Data.DataRow,System.Dec imal,VB$AnonymousType_0`10[System.Decimal,System.DateTime,System.DateTime,Sys tem.DateTime,System.DateTime,System.Decimal,System .Decimal,System.String,System.String,System.String]]'
to type 'System.Data.DataSet'."
The appilcation was working fine till yesterday, now it gives me the error,I am not sure why. I did not change anything. I left the code as it is on weekend and today after coming back, it gives error. could you please help me. thank you.
Unable to cast object of type '<JoinIterator>d__61`4[System.Data.DataRow,System.Data.DataRow,System.Dec imal,VB$AnonymousType_0`10[System.Decimal,System.DateTime,System.DateTime,Sys tem.DateTime,System.DateTime,System.Decimal,System .Decimal,System.String,System.String,System.String]]'
to type 'System.Data.DataSet'."
Hello:)
According to the err description, it seems that you are wanting to convert from an anoymous class instance to a DataSet, isn't it?
My suggestion at present is that you should directly bind the anoymous class instance to the data presentation control or create a DataSet and use for each to loop each item in the anoymous class instance and assign to the DataSet one by one.
Member
29 Points
127 Posts
how to assign DataRelation dataset as a source to a gridview
Jun 28, 2011 05:16 PM|user2980|LINK
HI there,
i am using DataRelation to join two datatables. here is the code.
Contributor
2808 Points
1815 Posts
Re: how to assign DataRelation dataset as a source to a gridview
Jun 29, 2011 08:28 AM|jeyaseelan@ajsquare.net|LINK
here is an related article
J.Jeyaseelan
Member
29 Points
127 Posts
Re: how to assign DataRelation dataset as a source to a gridview
Jun 29, 2011 03:14 PM|user2980|LINK
hi, thank you for your reply. But that did not help me much. I have finished pretty much what was in that article.
I have issue getting the child table contents and parent table contents into one table.
Could you please let me know if there is a way to join two data tables.
I tried using LINQ but I am getting error.
thank you
All-Star
94130 Points
18109 Posts
Re: how to assign DataRelation dataset as a source to a gridview
Jun 29, 2011 10:39 PM|Decker Dong - MSFT|LINK
Hello, you can change to this——
Dim joinquery = From t1 In dt1.AsEnumerable(), _
From t2 in dt2.AsEnumerable(), _
Where t1["FieldName"].ToString()==t2["FieldName"].ToString()
Select New With
{
Key .NewField = t1["FieldName1"],
…………………………
}
NewDg.DataSource = joinquery
NewDg.DataBind()
Member
29 Points
127 Posts
Re: how to assign DataRelation dataset as a source to a gridview
Jun 30, 2011 11:13 AM|user2980|LINK
thank you for your reply.
I get this new error,
System.Web.HttpException: The data source for GridView with id '' did not have any properties or attributes from which to generate columns. Ensure that your data source has content.
I checked for the content in joinquery and it has the rows of the two tables, t1 and t2. So I am not sure why it is not binding the data.
thank you
All-Star
94130 Points
18109 Posts
Re: how to assign DataRelation dataset as a source to a gridview
Jun 30, 2011 08:56 PM|Decker Dong - MSFT|LINK
Hello again:)
Sorry……
Please try to convert each of the field's value into a right type——Sample:
Key .NewField = t1.Field(Of Long)("FieldName")
Member
29 Points
127 Posts
Re: how to assign DataRelation dataset as a source to a gridview
Jul 05, 2011 11:09 AM|user2980|LINK
thank you....that did work...
thanks once agian..
Member
29 Points
127 Posts
Re: how to assign DataRelation dataset as a source to a gridview
Jul 05, 2011 02:42 PM|user2980|LINK
@decker,
The appilcation was working fine till yesterday, now it gives me the error,I am not sure why. I did not change anything. I left the code as it is on weekend and today after coming back, it gives error. could you please help me. thank you.
Unable to cast object of type '<JoinIterator>d__61`4[System.Data.DataRow,System.Data.DataRow,System.Dec imal,VB$AnonymousType_0`10[System.Decimal,System.DateTime,System.DateTime,Sys tem.DateTime,System.DateTime,System.Decimal,System .Decimal,System.String,System.String,System.String]]' to type 'System.Data.DataSet'."
All-Star
94130 Points
18109 Posts
Re: how to assign DataRelation dataset as a source to a gridview
Jul 05, 2011 09:06 PM|Decker Dong - MSFT|LINK
Hello:)
According to the err description, it seems that you are wanting to convert from an anoymous class instance to a DataSet, isn't it?
My suggestion at present is that you should directly bind the anoymous class instance to the data presentation control or create a DataSet and use for each to loop each item in the anoymous class instance and assign to the DataSet one by one.
Thx
Member
29 Points
127 Posts
Re: how to assign DataRelation dataset as a source to a gridview
Jul 06, 2011 10:08 AM|user2980|LINK
thank you Decker..that helped..sorry the code was huge and could not post it....