2012-09-07 112 views
0

我有一個listview顯示來自和sql server視圖的記錄。 數據由customerid進行過濾,並在視圖運行時具有2個唯一記錄。 但是,listview將只顯示第一條記錄,並顯示它兩次。 因此,有兩個記錄顯示在列表視圖中,但它不顯示視圖生成的兩個記錄,它只是在視圖中顯示第一條記錄兩次。 我希望這很清楚。listview只顯示一個記錄兩次

這是怎麼回事?我一直在尋找幾個小時,但沒有運氣。

<asp:ListView ID="lstBeforeAfter" runat="server" DataKeyNames="CustomerID" InsertItemPosition="None"> 
    <LayoutTemplate> 
     <div class="divreg"> 
      <table ID="itemPlaceholderContainer" runat="server" cellpadding="2" class="tablebasic" style="vertical-align:top"> 
       <tr ID="Tr1" runat="server" align="center"> 
        <td style="background-color:#7e0d7d; color:White; font-size:1.2em; font-weight:bold; padding-top:4px; padding-bottom:4px"> 
         Services 
        </td> 
       </tr> 
       <tr ID="itemPlaceholder" runat="server"> 
       </tr> 
      </table> 
     </div> 
     </td> 
    </LayoutTemplate>  

    <ItemTemplate> 
     <tr style=""> 

      <td align="center"> 
       <div class="divwrap"> 
       <div class="left3ColHeader"> 
        <b>Date</b> 
       </div> 
       <div class="mid3ColHeader"> 
        <b>Stylist</b> 
       </div> 
       <div class="right3ColHeader"> 
        <b>Services</b> 
       </div> 
        <div class="formclear"> 
        </div>      
       <div class="left3ColBorder"> 
        <%# Eval("SchedStartDateTime", "{0:d}")%> 
       </div> 
       <div class="mid3ColBorder"> 
        <%#Eval("FullName")%> 
       </div> 
       <div class="right3ColBorder"> 
        <%#Eval("ServiceText")%> 
       </div> 
        <div class="formclear"> 
        </div> 

       <div class="left2ColHeader"> 
        <b>Before Picture</b> 
       </div> 
       <div class="right2ColHeader"> 
        <b>After Picture</b> 
       </div> 
       <div class="formclear"> 
        </div> 



       <div class="left2ColPicBorder"> 

       <asp:Image ID="imgLeft" ImageUrl='<%# Eval("BeforePicPath") %>' runat="server" /> 
       </div> 
       <div class="right2ColPicBorder"> 
        <asp:Image runat="server" ID="imgRight" ImageUrl='<%# Eval("AfterPicPath") %>' /> 
       </div> 


       <div class="formclear"> 
        </div>  

       <div class="left2ColBorder"> 
        <asp:Label ID="lblBefore" Width="95%" runat="server" Text='<%# Eval("BeforePicName") %>'></asp:Label> 
       </div> 
       <div class="right2ColBorder"> 
        <asp:Label ID="lblAfter" Width="95%" runat="server" Text='<%# Eval("AfterPicName") %>'></asp:Label> 
       </div> 
       <div class="formclear"> 
        </div> 
       <div class="divreg"> 
        <hr /> 
       </div> 
        <div class="formclear"> 
        </div>  
       </div> 
       </td> 

     </tr> 
    </ItemTemplate> 
    <AlternatingItemTemplate> 
     <tr style="background-color:#ffffd9"> 

      <td align="center"> 
       <div class="divwrap"> 
       <div class="left3ColHeader"> 
        <b>Date</b> 
       </div> 
       <div class="mid3ColHeader"> 
        <b>Stylist</b> 
       </div> 
       <div class="right3ColHeader"> 
        <b>Services</b> 
       </div> 
        <div class="formclear"> 
        </div>      
       <div class="left3ColBorder"> 
        <%# Eval("SchedStartDateTime", "{0:d}")%> 
       </div> 
       <div class="mid3ColBorder"> 
        <%#Eval("FullName")%> 
       </div> 
       <div class="right3ColBorder"> 
       <%# Eval("ServiceText").ToString%> 
       </div> 

        <div class="formclear"> 
        </div> 

       <div class="left2ColHeader"> 
        <b>Before Picture</b> 
       </div> 
       <div class="right2ColHeader"> 
        <b>After Picture</b> 
       </div> 
       <div class="formclear"> 
        </div> 

       <div class="left2ColPicBorder"> 

       <asp:Image ID="imgLeft" ImageUrl='<%# Eval("BeforePicPath") %>' runat="server" /> 
       </div> 
       <div class="right2ColPicBorder"> 
        <asp:Image runat="server" ID="imgRight" ImageUrl='<%# Eval("AfterPicPath") %>' /> 
       </div> 


       <div class="formclear"> 
        </div>  

       <div class="left2ColBorder"> 
        <asp:Label ID="lblBefore" Width="95%" runat="server" Text='<%# Eval("BeforePicName") %>'></asp:Label> 
       </div> 
       <div class="right2ColBorder"> 
        <asp:Label ID="lblAfter" Width="95%" runat="server" Text='<%# Eval("AfterPicName") %>'></asp:Label> 
       </div> 
       <div class="formclear"> 
        </div> 
       <div class="divreg"> 
        <hr /> 
       </div> 
        <div class="formclear"> </div>  
       </div> 
       </td>    
     </tr>   
    </AlternatingItemTemplate> 
</asp:ListView> 

Protected Sub LoadPortfolio(ByVal iCustID As Integer) 
    Dim dc As New SalonDataClassesDataContext 


    Dim q = From p In dc.vClientStyles Where p.CustomerID = iCustID Order By p.SchedStartDateTime Descending Select p 
    If q.Count > 0 Then 
     Me.lstBeforeAfter.DataSource = q 
     Me.lstBeforeAfter.DataBind() 

    End If 


End Sub 

SELECT ISNULL(dbo.Services.ServiceID, 0) AS ServiceID, dbo.Schedules.SchedStartDateTime, dbo.Schedules.ServiceText, dbo.Schedules.CustomerID, 
        ISNULL(dbo.Services.Completed, 0) AS Completed, dbo.Stylists.FullName, dbo.Stylists.StylistID, ISNULL(dbo.Services.BeforePicName, 'No Title') AS BeforePicName, 
        ISNULL(dbo.Services.BeforePicPath, '../images2020/resume-photo.jpg') AS BeforePicPath, ISNULL(dbo.Services.AfterPicName, 'No Title') AS AfterPicName, 
        ISNULL(dbo.Services.AfterPicPath, '../images2020/resume-photo.jpg') AS AfterPicPath 
FROM dbo.Schedules INNER JOIN dbo.Stylists ON dbo.Schedules.UserID = dbo.Stylists.UserID LEFT OUTER JOIN dbo.Services ON dbo.Schedules.ServiceID = dbo.Services.ServiceID 
ORDER BY dbo.Schedules.SchedStartDateTime DESC 

更改爲聯合查詢,但linq不喜歡它。

SELECT ISNULL(dbo.Schedules.ServiceID, 0) AS ServiceID,  dbo.Schedules.SchedStartDateTime, dbo.Schedules.ServiceText, dbo.Schedules.CustomerID, 
ISNULL(dbo.Schedules.Completed, 0) AS Completed, dbo.Stylists.FullName,  dbo.Stylists.StylistID, 'No Title' AS BeforePicName, '../images2020/resume-photo.jpg' AS  BeforePicPath, 'No Title' AS AfterPicName, '../images2020/resume-photo.jpg' AS AfterPicPath 
FROM dbo.Schedules INNER JOIN dbo.Stylists ON dbo.Schedules.UserID = dbo.Stylists.UserID 
Where dbo.Schedules.CustomerID = 27 
UNION 
SELECT ISNULL(dbo.Services.ServiceID, 0) AS ServiceID,   dbo.Schedules.SchedStartDateTime, dbo.Schedules.ServiceText, dbo.Schedules.CustomerID, 
ISNULL(dbo.Services.Completed, 0) AS Completed, dbo.Stylists.FullName,  dbo.Stylists.StylistID, ISNULL(dbo.Services.BeforePicName, 'No Title') AS BeforePicName, 
ISNULL(dbo.Services.BeforePicPath, '../images2020/resume-photo.jpg') AS BeforePicPath, ISNULL(dbo.Services.AfterPicName, 'No Title') AS AfterPicName,  ISNULL(dbo.Services.AfterPicPath, '../images2020/resume-photo.jpg') AS AfterPicPath 
FROM dbo.Schedules INNER JOIN dbo.Stylists ON dbo.Schedules.UserID = dbo.Stylists.UserID INNER JOIN dbo.Services ON dbo.Schedules.ServiceID = dbo.Services.ServiceID 
Where dbo.Schedules.CustomerID = 27 ORDER BY dbo.Schedules.SchedStartDateTime DESC 

我現在得到錯誤...查詢結果不能被枚舉多次。

我在做什麼錯? 感謝

回答

0

編輯3個

中的DataKeyNames返回的數據必須是唯一的。您擁有相同的CustomerID這一事實意味着您需要另一列來創建唯一的密鑰。理論上你可以在DataKeyNames中有CustomerID和ServiceID,但是你有一些空的ServiceID記錄這一事實使得它很難。

爲什麼不爲每個名爲「MyUniqueID」的選擇添加一個新列並將其值設置爲NewId()。然後使用MyUniqueID作爲DataKeyNames。

SELECT NewId() as MyUniqueID from blah 

的NEWID()函數將確保您獲得全球unqiue標識符在結果集的每一行,但因爲數據沒有在數據庫中,以便將丟失堅持不使用UniqueID的任何後續操作每次ListView被刷新。

很難理解你爲什麼在你的例子中使用聯合主要是由於它很難格式化stackoverflow代碼塊。但是很明顯,您有多次相同的客戶ID,因此它不能用作唯一的DataKeyName。

或者也許CustomerID + UserIDSchedules是獨一無二的,應該在DataKeyNames中合併??

指定在的DataKeyNames多列如下: -

DataKeysNames="CustomerID,UserID,ServiceID" 

如果這不起作用,你應該解釋一下你希望返回到您的ListView單個CustomerID什麼獨特的性能每個記錄在哪該ListView將有

取出工會,你的第一個例子似乎最好的工作,除了獨特

+0

不,他們有個相同的ID。我正在做一個select語句where customerid = customerid –

+0

好的,我也將視圖添加到原始問題 –

+0

感謝您的幫助 –