2014-07-07 26 views
0

我在Visual Studio 2010中創建了一個網站。在網頁上,我想在gridview中顯示日期。當我去配置數據源時,我使用查詢生成器來獲取我想要的數據 - 即,我想顯示當前登錄的用戶的所有記錄。用戶從Windows登錄確定。我的選擇語句是:在網站的Gridview中選擇聲明不起作用

SELECT tblWorkHours.EmployeeName, tblWorkHours.BeginDateOff, tblWorkHours.EndDateOff,  
tblWorkHours.AllDay_YesNo, tblWorkHours.BeginTimeOff, tblWorkHours.EndTimeOff, 
tblWorkHours.Approved, tblCodesWork.Description, tblEmployees.Login FROM tblWorkHours INNER JOIN 
tblCodesWork ON tblWorkHours.WorkCode = tblCodesWork.WorkCodeID INNER JOIN tblEmployees ON 
tblWorkHours.Employee = tblEmployees.EmployeeID WHERE (tblEmployees.Login = @username) ORDER BY 
tblWorkHours.BeginDateOff DESC 

當我在配置數據源嚮導中測試查詢時,查詢返回正確的數據。當我運行網頁時,沒有gridview。我已經嘗試在網頁中添加一個字段,以確保我獲得了正確的用戶名,並且工作正常。我究竟做錯了什麼?任何幫助將不勝感激!

這裏是我的網頁代碼:

<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" 
CodeBehind="TimeOffAllByUser.aspx.vb" Inherits="timework.TimeOffAllByUser" %> 
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server"> 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 
<asp:GridView ID="GridView1" runat="server" 
    DataSourceID="SqlDataSource1" AllowPaging="True" AllowSorting="True"> 
    <Columns> 
     <asp:BoundField DataField="EmployeeName" HeaderText="EmployeeName" 
      SortExpression="EmployeeName"> 
     </asp:BoundField> 
     <asp:BoundField DataField="BeginDateOff" 
      HeaderText="BeginDateOff" SortExpression="BeginDateOff"> 
     </asp:BoundField> 
     <asp:BoundField DataField="EndDateOff" 
      HeaderText="EndDateOff" SortExpression="EndDateOff"> 
     </asp:BoundField> 
     <asp:CheckBoxField DataField="AllDay_YesNo" HeaderText="AllDay_YesNo" 
      SortExpression="AllDay_YesNo"> 
     </asp:CheckBoxField> 
     <asp:BoundField DataField="BeginTimeOff" 
      HeaderText="BeginTimeOff" SortExpression="BeginTimeOff"> 
     </asp:BoundField> 
     <asp:BoundField DataField="EndTimeOff" 
      HeaderText="EndTimeOff" SortExpression="EndTimeOff"> 
     </asp:BoundField> 
     <asp:CheckBoxField DataField="Approved" HeaderText="Approved" 
      SortExpression="Approved"> 
     </asp:CheckBoxField> 
     <asp:BoundField DataField="Description" HeaderText="Description" 
      SortExpression="Description"> 
     </asp:BoundField> 
     <asp:DynamicField DataField="Login" HeaderText="Login" /> 
    </Columns> 
</asp:GridView> 

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:TimeSQLConnectionString1 %>" 
    SelectCommand="SELECT tblWorkHours.EmployeeName, tblWorkHours.BeginDateOff, 
    tblWorkHours.EndDateOff, tblWorkHours.AllDay_YesNo, tblWorkHours.BeginTimeOff, 
    tblWorkHours.EndTimeOff, tblWorkHours.Approved, tblCodesWork.Description, tblEmployees.Login 
    FROM tblWorkHours INNER JOIN tblCodesWork ON tblWorkHours.WorkCode = tblCodesWork.WorkCodeID 
    INNER JOIN tblEmployees ON tblWorkHours.Employee = tblEmployees.EmployeeID WHERE 
    (tblEmployees.Login = @username) ORDER BY tblWorkHours.BeginDateOff DESC" > 
    <SelectParameters> 
     <asp:QueryStringParameter Name="username" 
      QueryStringField="Split(HttpContext.Current.User.Identity.Name, quot;\&quot;, , 
      CompareMethod.Text)" 
      Type="String" /> 
    </SelectParameters> 
</asp:SqlDataSource> 

VB代碼是:

Imports System 
    Imports System.Data 
    Imports System.Data.OleDb 
    Imports System.Data.SqlClient 
    Imports System.Configuration 
    Imports System.Web.Mvc 
    Public Class TimeOffAllByUser 
     Inherits System.Web.UI.Page 
     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 

      Dim vusername() As String 

      vusername = Split(HttpContext.Current.User.Identity.Name, "\", , CompareMethod.Text) 
      'TextBox1.Text = (vusername(1)) 

     End Sub 

End Class 
+0

只是爲了檢查,你可以這樣改變,以瞭解這是否是問題報價? QueryStringField ='分割(HttpContext.Current.User.Identity.Name,「\」,, CompareMethod.Text)' –

+0

不 - 沒有工作。感謝您的建議。還試過: – user3033348

+0

糟糕 - 還試過:QueryStringField ='拆分(HttpContext.Current.User.Identity.Name," \ " ,, CompareMethod.Text)' – user3033348

回答

0

好吧,我認爲這個問題是您正在使用QueryStringParameter並在querystring字段你正在使用的表達式,這可能無法正常工作。你可以做的反而是

您QueryStringParameter更改爲正常的參數

<asp:Parameter Name="username" /> 

在您的數據源declarcation添加選擇事件

OnSelecting="SqlDataSource1_Selecting" 

在選擇事件指派參數值

Protected Sub SqlDataSource1_Selecting(ByVal sender as Object, ByVal e As SqlDataSourceSelectingEventArgs) 

    Dim vusername() As String 
    vusername = Split(HttpContext.Current.User.Identity.Name, "\", , CompareMethod.Text) 
    e.Command.Parameters["@username"].Value=vusername(1) 
End Sub 
+0

我使用VB,我有問題翻譯這從c to vb。你可以幫我嗎? – user3033348

+0

我已經編輯了答案,希望這個作品可以用 –

+0

它有效!我只是稍微調整了一下。 e.Command.Parameters(「@ username」)。Value = vusername(1) - 圓括號不括號。非常感謝!!! – user3033348