在数据库中发送用户名

时间:2013-01-02 20:41:08

标签: asp.net vb.net

我有一个详细信息视图,其中我启用了“编辑/新建”。在“新建”,以及将在数据库行中的其他额外信息,即时尝试也发送登录的当前用户名。目前,它保持无效。

这是我的代码:

<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="700px" 
        AutoGenerateRows="False" DataKeyNames="ID" 
        DataSourceID="AccessDataSource2" AllowPaging="True" CssClass="mdtlstblwithhlines">
        <Fields>
            <asp:TemplateField HeaderText="Rating" SortExpression="Rating">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Rating") %>'></asp:TextBox>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Rating") %>'></asp:TextBox>
                </InsertItemTemplate>
                <ItemTemplate>
                 <asp:Rating ID="Rating1" CurrentRating= '<%# Bind("Rating") %>'
                        runat="server" EmptyStarCssClass="EmptyStar" 
                        FilledStarCssClass="FilledStar" StarCssClass="Star" 
                        WaitingStarCssClass="WaitingStar" AutoPostBack="True" Enabled="True" 
                        EnableTheming="True" ReadOnly="True">
                    </asp:Rating>

                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="TheGood" HeaderText="TheGood" 
                SortExpression="TheGood" />
            <asp:BoundField DataField="TheBad" HeaderText="TheBad" 
                SortExpression="TheBad" />
            <asp:BoundField DataField="TheBottomLine" HeaderText="TheBottomLine" 
                SortExpression="TheBottomLine" />
            <asp:BoundField DataField="DatePosted" HeaderText="DatePosted" 
                SortExpression="DatePosted" />
            <asp:BoundField DataField="LastUpdate" HeaderText="LastUpdate" 
                SortExpression="LastUpdate" />
            <asp:TemplateField HeaderText="UserName" SortExpression="UserName" Visible="False">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("UserName") %>'></asp:TextBox>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("UserName") %>' ></asp:TextBox>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("UserName") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:CommandField ShowEditButton="True" ShowInsertButton="True" />
        </Fields>
    </asp:DetailsView>

   </div>


<asp:AccessDataSource ID="AccessDataSource2" runat="server" 
    DataFile="~/App_Data/ReviewSite.accdb" 
    SelectCommand="SELECT * FROM [ReviewsCam] WHERE ([ItemID] = ?)" 
        DeleteCommand="DELETE FROM [ReviewsCam] WHERE [ID] = ?" 
        InsertCommand="INSERT INTO [ReviewsCam] ([ItemID], [UserID], [UserName], [Rating], [TheGood], [TheBad], [TheBottomLine], [DatePosted], [LastUpdate]) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)" 
        UpdateCommand="UPDATE [ReviewsCam] SET [ItemID] = ?, [UserID] = ?, [UserName] = ?, [Rating] = ?, [TheGood] = ?, [TheBad] = ?, [TheBottomLine] = ?, [DatePosted] = ?, [LastUpdate] = ? WHERE [ID] = ?">
    <DeleteParameters>
        <asp:Parameter Name="ID" Type="Int32" />
    </DeleteParameters>
    <InsertParameters>
        <asp:Parameter Name="ItemID" Type="Int32" />
        <asp:Parameter Name="UserID" Type="String" />
        <asp:Parameter Name="UserName" Type="String" />
        <asp:Parameter Name="Rating" Type="Int32" />
        <asp:Parameter Name="TheGood" Type="String" />
        <asp:Parameter Name="TheBad" Type="String" />
        <asp:Parameter Name="TheBottomLine" Type="String" />
        <asp:Parameter Name="DatePosted" Type="DateTime" />
        <asp:Parameter Name="LastUpdate" Type="DateTime" />
    </InsertParameters>
    <SelectParameters>
        <asp:QueryStringParameter Name="ItemID2" QueryStringField="ID" Type="Int32" />
    </SelectParameters>
    <UpdateParameters>
        <asp:Parameter Name="ItemID" Type="Int32" />
        <asp:Parameter Name="UserID" Type="String" />
        <asp:Parameter Name="UserName" Type="String" />
        <asp:Parameter Name="Rating" Type="Int32" />
        <asp:Parameter Name="TheGood" Type="String" />
        <asp:Parameter Name="TheBad" Type="String" />
        <asp:Parameter Name="TheBottomLine" Type="String" />
        <asp:Parameter Name="DatePosted" Type="DateTime" />
        <asp:Parameter Name="LastUpdate" Type="DateTime" />
        <asp:Parameter Name="ID" Type="Int32" />
    </UpdateParameters>
</asp:AccessDataSource>

2 个答案:

答案 0 :(得分:2)

试试这个

  Public Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs )

        SqlDataSource1.InsertParameters("UserName").DefaultValue = User.Identity.Name

  End Sub

答案 1 :(得分:1)

使用会话变量将用户名存储在代码中,如此

Dim crntuser As String = ""
crntuser = Login1.UserName.ToString
Session.Add("User", crntuser)


<InsertParameters>
    <asp:Parameter Name="ItemID" Type="Int32" />
    <asp:Parameter Name="UserID" Type="String" />
    <asp:SessionParameter Name="UserName" SessionField="User" Type="String" />
    <asp:Parameter Name="Rating" Type="Int32" />
    <asp:Parameter Name="TheGood" Type="String" />
    <asp:Parameter Name="TheBad" Type="String" />
    <asp:Parameter Name="TheBottomLine" Type="String" />
    <asp:Parameter Name="DatePosted" Type="DateTime" />
    <asp:Parameter Name="LastUpdate" Type="DateTime" />
</InsertParameters>