将User.Identity.Name绑定到gridview

时间:2018-04-06 18:05:23

标签: sql asp.net vb.net

ASP.NET和SQL新手。我很难弄清楚如何将User.Identity.Name绑定到gridview。

当我使用查询测试器时,我得到了正确的数据,但是当我运行aspx页面时,Gridview根本没有出现。

SELECT Payment.PAY_RECEIVED, Payment.PAYMENT_DATE, Payment.PAYMENT_SUMMARY, 
Payment.USER_NAME, Schedule.SCHEDULE_ID, Schedule.SERVICE_DATE, 
Schedule.EMAIL, Schedule.ADDRESS, Schedule.FIRST_NAME, 
              Schedule.LAST_NAME, Schedule.USER_NAME AS Expr1
FROM     Payment INNER JOIN
              Schedule ON Payment.USER_NAME = Schedule.USER_NAME
WHERE  (Payment.USER_NAME = @USER_NAME)

VB代码

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1" DataKeyNames="PAID_ID">
    <Columns>
        <asp:BoundField DataField="PAID_ID" HeaderText="PAID_ID" InsertVisible="False" ReadOnly="True" SortExpression="PAID_ID" />
        <asp:BoundField DataField="PAY_RECEIVED" HeaderText="PAY_RECEIVED" SortExpression="PAY_RECEIVED" />
        <asp:BoundField DataField="PAYMENT_DATE" HeaderText="PAYMENT_DATE" SortExpression="PAYMENT_DATE" />
        <asp:BoundField DataField="PAYMENT_SUMMARY" HeaderText="PAYMENT_SUMMARY" SortExpression="PAYMENT_SUMMARY" />
        <asp:BoundField DataField="USER_NAME" HeaderText="USER_NAME" SortExpression="USER_NAME" />
    </Columns>
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DeleteMethod="Delete" InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" SelectMethod="GetDataByCustomer" TypeName="DataSetTableAdapters.PaymentTableAdapter" UpdateMethod="Update">
    <DeleteParameters>
        <asp:Parameter Name="Original_PAID_ID" Type="Int32" />
    </DeleteParameters>
    <InsertParameters>
        <asp:Parameter Name="PAY_RECEIVED" Type="String" />
        <asp:Parameter Name="PAYMENT_DATE" Type="DateTime" />
        <asp:Parameter Name="PAYMENT_SUMMARY" Type="String" />
        <asp:Parameter Name="USER_NAME" Type="String" />
    </InsertParameters>
    <SelectParameters>
        <asp:ProfileParameter DefaultValue="e.InputParamater[&quot;USER_NAME&quot;] = HttpContext.Current.User.Name" Name="USER_NAME" PropertyName="USER_NAME" Type="String" />
    </SelectParameters>

如何让对象数据源读取User.Identity.Name?

1 个答案:

答案 0 :(得分:0)

找出解决方案。首先,我从对象数据源切换到SQL。

    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString %>" SelectCommand="SELECT * FROM [Schedule] WHERE ([USER_NAME] = @USER_NAME)">
    <SelectParameters>
        <asp:Parameter Name="USER_NAME" Type="String" />
    </SelectParameters>
</asp:SqlDataSource>

进入aspx.vb页面并将其设置为魅力

    Private Sub SqlDataSource2_Selecting(sender As Object, e As SqlDataSourceSelectingEventArgs) Handles SqlDataSource2.Selecting
    e.Command.Parameters("@USER_NAME").Value = User.Identity.Name
End Sub