如何在select查询中使用distinct关键字在嵌套转发器中显示ASP.NET C# [英] How to use distinct keyword in select query to display in nested repeaters ASP.NET C#

查看:89
本文介绍了如何在select查询中使用distinct关键字在嵌套转发器中显示ASP.NET C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示名称一次,并且该特定名称具有值,但我将使用值重复获取名称。我正在c#asp.net中设计在线考试系统,其中学生有问题有嵌套中继器。外部中继器正在获取主题主题名称,而内部中继器正在提供可供选择的选项的问题。我的问题是主题主题名称正在重复,问题意味着第一个问题是主题名称'辅助动词'。这个主题名称有10个问题,这10个问题我得到辅助动词10次,我不需要。如何实现这一点请任何人回答我,我将分享您使用的查询,嵌套中继器和编码部分。请看看并为我提供适当的解决方案。



我尝试过:



Sql Query选择命令:

 string query =SELECT DISTINCT SubjectCategory,QuestionNumber FROM QuestionPaper; 





Nested Repeater Aspx页面:

< asp:Repeater ID =RptrSubjectCategoryOnItemDataBound =RptrSubjectCategory_ItemDataBoundrunat =server > 
< HeaderTemplate>
< table style =min-height:300px; width:100%;>
< / HeaderTemplate>
< SeparatorTemplate>
< tr>< td>< br />< / td>< / tr>
< / SeparatorTemplate>
< ItemTemplate>
< tr style =text-align:left;>
< th>主题:  <%#DataBinder.Eval(Container.DataItem,SubjectCategory)%> < / th>
< / tr>

<% - 带选择的转发器问题 - %>
< asp:Repeater ID =RptrQuestionsAdnChoicesOnItemDataBound =RptrQuestionsAdnChoices_ItemDataBoundrunat =serverDataSource ='<%#((System.Data.DataRowView)Container.DataItem).Row.GetChildRows(myRelation )%>'>
< ItemTemplate>
< tr>
< td style =padding:5px;>
< asp:Label ID =lblQuestionNuumberrunat =serverText ='<%#DataBinder.Eval(Container.DataItem,[\QuestionNumber \])%>' >< / ASP:标签>
  <%#DataBinder.Eval(Container.DataItem,[\Question\])%>
< / td>
< / tr>
< tr>
< td style =padding:5px;>
< asp:RadioButton ID =rb1runat =server/>
A)< asp:Label ID =lblChoiceArunat =serverText ='<%#DataBinder.Eval(Container.DataItem,[\ChoiceA \])%> ;'AssociatedControlID =rb1>< / asp:Label>
< / td>
< / tr>
< tr>
< td style =padding:5px;>
< asp:RadioButton ID =rb2runat =server/>
B)< asp:Label ID =lblChoiceBrunat =serverText ='<%#DataBinder.Eval(Container.DataItem,[\ChoiceB \])%> ;'AssociatedControlID =rb2>< / asp:Label>
< / td>
< / tr>
< tr>
< td style =padding:5px;>
< asp:RadioButton ID =rb3runat =server/>
C)< asp:Label ID =lblChoiceCrunat =serverText ='<%#DataBinder.Eval(Container.DataItem,[\ChoiceC \])%> ;'AssociatedControlID =rb3>< / asp:Label>
< / td>
< / tr>
< tr>
< td style =padding:5px;>
< asp:RadioButton ID =rb4runat =server/>
D)< asp:Label ID =lblChoiceDrunat =serverText ='<%#DataBinder.Eval(Container.DataItem,[\ChoiceD \])%> ;'AssociatedControlID =rb4>< / asp:Label>
< / td>
< / tr>
< / ItemTemplate>
< / asp:Repeater>
< / ItemTemplate>
< FooterTemplate>
< tr style =text-align:left;>
< td style =padding:5px;>
< asp:LinkBut​​ton ID =LnkBtnSubmitrunat =serverCssClass =btn btn-success> 完成< / ASP:LinkBut​​ton的>
< / td>
< / tr>
< / table>
< / FooterTemplate>
< / asp:Repeater>

解决方案

引用:

这个主题名称有10个问题,这10个问题我得到辅助动词10次,我不需要。



查询的问题在于您已包含 QuestionNumber 列。您实际上不太可能想要问题编号,所以只需将其留空

  SELECT   DISTINCT  SubjectCategory  FROM  QuestionPaper 

或者,您可以使用GROUP BY并返回有意义的内容,例如每个类别中的问题数量,例如

  SELECT  SubjectCategory,COUNT(QuestionNumber) as  NoOfQuestions  FROM  QuestionPaper  GROUP   BY  SubjectCategory 


I want to display the name once and that particular name having values but i am getting the name repeatedly with the values. I am designing Online Examination system in c# asp.net in which Students Questions having Nested Repeaters. Outer Repeater is getting the Subject Topic Name and Inner Repeater is having Questions with options to select. My problem is Subject topic name is getting repeated with the questions that means first question is having topic name 'Auxiliary Verbs'. This topic name has 10 questions for this 10 questions i am getting Auxiliary Verbs 10 times that i don't require. How to achieve this please anybody answer me i will share you query i have used, Nested Repeater and coding part. Please have a look and give me appropriate solution for this problem.

What I have tried:

Sql Query Select Command:

string query = "SELECT DISTINCT SubjectCategory, QuestionNumber FROM QuestionPaper";



Nested Repeater Aspx Page:

<asp:Repeater ID="RptrSubjectCategory" OnItemDataBound="RptrSubjectCategory_ItemDataBound" runat="server">
    <HeaderTemplate>
        <table style="min-height:300px;width:100%;">
    </HeaderTemplate>
    <SeparatorTemplate>
        <tr><td><br /></td></tr>
    </SeparatorTemplate>
    <ItemTemplate>
        <tr style="text-align:left;">
            <th>Topic: <%# DataBinder.Eval(Container.DataItem,"SubjectCategory") %></th>
        </tr>

        <%-- Repeater Questions With Choices --%>
        <asp:Repeater ID="RptrQuestionsAdnChoices" OnItemDataBound="RptrQuestionsAdnChoices_ItemDataBound" runat="server" DataSource='<%# ((System.Data.DataRowView)Container.DataItem).Row.GetChildRows("myRelation") %>'>
            <ItemTemplate>
                <tr>
                    <td style="padding:5px;">
                        <asp:Label ID="lblQuestionNuumber" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "[\"QuestionNumber\"]") %>'></asp:Label>
                          <%# DataBinder.Eval(Container.DataItem, "[\"Question\"]") %>
                    </td>
                </tr>
                <tr>
                    <td style="padding:5px;">
                        <asp:RadioButton ID="rb1" runat="server" />
                        A) <asp:Label ID="lblChoiceA" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "[\"ChoiceA\"]") %>' AssociatedControlID="rb1"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td style="padding:5px;">
                        <asp:RadioButton ID="rb2" runat="server" />
                        B) <asp:Label ID="lblChoiceB" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "[\"ChoiceB\"]") %>' AssociatedControlID="rb2"></asp:Label>                                                                
                    </td>
                </tr>
                <tr>
                    <td style="padding:5px;">
                        <asp:RadioButton ID="rb3" runat="server" />
                        C) <asp:Label ID="lblChoiceC" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "[\"ChoiceC\"]") %>' AssociatedControlID="rb3"></asp:Label>                                                             
                    </td>
                </tr>
                <tr>
                    <td style="padding:5px;">
                        <asp:RadioButton ID="rb4" runat="server" />
                        D) <asp:Label ID="lblChoiceD" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "[\"ChoiceD\"]") %>' AssociatedControlID="rb4"></asp:Label>                                                       
                    </td>
                </tr>
            </ItemTemplate>
        </asp:Repeater>
    </ItemTemplate>
    <FooterTemplate>
        <tr style="text-align:left;">
            <td style="padding:5px;">
                <asp:LinkButton ID="LnkBtnSubmit" runat="server" CssClass="btn btn-success">Finish</asp:LinkButton>
            </td>
        </tr>
        </table>
    </FooterTemplate>
</asp:Repeater>

解决方案

Quote:

This topic name has 10 questions for this 10 questions i am getting Auxiliary Verbs 10 times that i don't require.


The problem with your query is that you have included the QuestionNumber column. It is unlikely that you actually want the question number so just leave it out

SELECT DISTINCT SubjectCategory FROM QuestionPaper

Alternatively you can use GROUP BY and return something meaningful such as the number of questions in each category e.g.

SELECT SubjectCategory, COUNT(QuestionNumber) as NoOfQuestions FROM QuestionPaper GROUP BY SubjectCategory


这篇关于如何在select查询中使用distinct关键字在嵌套转发器中显示ASP.NET C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆