检索当前的UserId并插入表中?

时间:2012-03-16 04:54:48

标签: asp.net vb.net

使用以下代码,用户在文本框控件中输入日期,然后单击按钮。在表[rms_Report]中创建一条新记录,然后检索刚刚添加的记录的ID。我将此ID传递到页面" CreateReport.aspx"通过response.redirect和查询字符串。

我需要检索当前登录用户的UserId并将其插入表[rms_Report]。我需要在插入新记录之后发生这种情况,但显然在response.redirect之前。我知道如何检索UserId,但我不确定如何合并到其他代码中。任何人都可以提供一些见解吗?

ASPX

  <asp:SqlDataSource ID="ReportDataSource" runat="server" 
        OnInserted="ReportDataSource_Inserted"
        ConflictDetection="CompareAllValues" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>"  
        InsertCommand="INSERT INTO [rms_Report] ([ReportDate]) VALUES (@ReportDate) SELECT @NewReportID = SCOPE_IDENTITY()" 
        OldValuesParameterFormatString="original_{0}">
        <InsertParameters>
            <asp:Parameter DbType="Date" Name="ReportDate" />
            <asp:Parameter Direction="Output" Name="NewReportID" Type="Int32" />
        </InsertParameters>
    </asp:SqlDataSource>

ASPX.VB

Protected Sub ReportDataSource_Inserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs)

    'Read the value of the @NewProductID OUTPUT parameter
    Dim newReportID As Integer = Convert.ToInt32(e.Command.Parameters("@NewReportID").Value)

    Response.Redirect("CreateReport.aspx?ReportID=" & newReportID)

End Sub

更新ASPX

   <asp:SqlDataSource ID="ReportDataSource" runat="server"
        OnInserted="ReportDataSource_Inserted"
        OnInserting="ReportDataSource_Inserting"
        ConflictDetection="CompareAllValues" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>"  
        InsertCommand="INSERT INTO [rms_Report] ([UserID], [ReportDate]) VALUES (@UserID, @ReportDate) SELECT @NewReportID = SCOPE_IDENTITY()" 
        OldValuesParameterFormatString="original_{0}">
        <InsertParameters>
            <asp:Parameter DbType="Date" Name="ReportDate" />
            <asp:Parameter DbType="Guid" Name="UserID" />
            <asp:Parameter Direction="Output" Name="NewReportID" Type="Int32" />
        </InsertParameters>
    </asp:SqlDataSource>

更新ASPX.VB

   Protected Sub ReportDataSource_Inserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs)

        Dim newReportID As Integer = Convert.ToInt32(e.Command.Parameters("@NewReportID").Value)

        Response.Redirect("CreateReport.aspx?ReportID=" & newReportID)

    End Sub

    Protected Sub ReportDataSource_Inserting(ByVal sender As Object, ByVal e As SqlDataSourceCommandEventArgs)

        Dim currentUser As MembershipUser = Membership.GetUser()
        Dim currentUserId As Guid = CType(currentUser.ProviderUserKey, Guid)
        e.Command.Parameters("@UserID").Value = currentUserId

    End Sub

2 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

<asp:SqlDataSource ID="ReportDataSource" runat="server" 
        OnInserted="ReportDataSource_Inserted"
        OnInserting="ReportDataSource_Inserting"
        ConflictDetection="CompareAllValues" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>"  
        InsertCommand="INSERT INTO [rms_Report] ([ReportDate]) VALUES (@ReportDate) SELECT @NewReportID = SCOPE_IDENTITY()" 
        OldValuesParameterFormatString="original_{0}">
        <InsertParameters>
            <asp:Parameter DbType="Date" Name="ReportDate" />
            <asp:Parameter Name="UserId" Type="String" />
            <asp:Parameter Direction="Output" Name="NewReportID" Type="Int32" />
        </InsertParameters>
    </asp:SqlDataSource>

在代码隐藏中,您可以使用OnInserting事件在对数据库实际运行命令之前更改参数的值。

Protected Sub ReportDataSource_Inserting(ByVal sender As Object, ByVal e As SqlDataSourceCommandEventArgs)
    e.Command.Parameters["@Userid"].Value = YourUserIdValue
 End Sub

答案 1 :(得分:0)

尝试使用此代码

dim loggedinuser as string = CStr(Page.User.Identity.Name).Split("\")(1)