Answered by:
Repeater with 2 different datasource -C#.NET

Question
-
User-1578974752 posted
Hi
I have a Repeater which have a table. Repeater is connected to Data source 1
But one row of the table (middle row) need to show the value from datasource2 (Dtaasource1 & Datasource 2 are from unlinked tables).
How can I make it possible. Is it possible? Appreciate the help
Thanks
Tuesday, November 17, 2020 5:45 AM
Answers
-
User-1578974752 posted
Hi KathyW
st.RP_BU is an unlinked table with different columns and datatypes. Hence I used CROSS JOIN. It works well.
Thanks for the suggestions.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, November 19, 2020 12:48 AM
All replies
-
User-1330468790 posted
Hi shsu,
A repeater cannot be assigned with two different data sources.
I presume that you want to bind columns with the same name from two unlinked tables.
There is a workaround that you could use 'UNION' to concatenates result sets from two queries so that you just need one data source for data binding.
More details, you could refer to below codes.
aspx:
<form id="form1" runat="server"> <div> <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1"> <HeaderTemplate> <table border="1"> <tr> <td><b>ID</b></td> <td><b>Name</b></td> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td> <asp:Label ID="label1" runat="server" Text='<%#Eval("ID") %>'></asp:Label> </td> <td> <asp:Label ID="label2" runat="server" Text='<%#Eval("Name") %>'></asp:Label></td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString%>" SelectCommand="SELECT ID, Name FROM CarImgTbl UNION ALL SELECT ID, Name FROM city_table"></asp:SqlDataSource> </div> </form>
Result:
If you do have other conditions that we need to consider, feel free to let me know.
Best regards,
Sean
Wednesday, November 18, 2020 6:11 AM -
User-1578974752 posted
Hi
Thanks
SELECT RNUMBER, DESCRIPTION FROM st.HEADERS_ALL a, st.DETAILS_ALL b WHERE a.initem_id = b.initem_id and RNUMBER = '005432'
above is the current working query.
I want to UNION ALL below statement
SELECT BU1, BU2, BU3,BU4 FROM st.RP_BU
Showing error when I did that.
Wednesday, November 18, 2020 9:34 AM -
User409696431 posted
Always tell us the error.
But I assume it's because UNION and UNION ALL require
- There must be same number of expressions in both SELECT statements.
- The corresponding columns in each of the SELECT statements must have similar data types.
You can solve the first problem by returning a dummy value of the correct type for the missing columns.
SELECT RNUMBER, DESCRIPTION FROM st.HEADERS_ALL Add two dummy columns that return NULL: SELECT RNUMBER, DESCRIPTION, NULL, NULL FROM st.HEADERS_ALL
Since the column names returned will be based on the first Select, you may want to alias the dummy columns if that SELECT is first.:
SELECT RNUMBER, DESCRIPTION, NULL AS BU3, NULL AS BU4 FROM st.HEADERS_ALL
If you have the second case you'd have to return dummy values in both Selects, lining up the columns to the correct type.
You'll have an interesting time in your Repeater EVALs: all columns will be returned for all rows (under the column name in the first SELECT), and you'll need to parse things in your EVAL's to display the correct ones.
Wednesday, November 18, 2020 5:35 PM -
User-1578974752 posted
Hi KathyW
st.RP_BU is an unlinked table with different columns and datatypes. Hence I used CROSS JOIN. It works well.
Thanks for the suggestions.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, November 19, 2020 12:48 AM