尝试获取DB插入参数的当前登录

时间:2016-08-02 15:57:47

标签: c# asp.net sql-server-2016

这是Home.aspx代码 - 一切正常,除了获取用户名:

<asp:SqlDataSource ID="SqlDataSource3" runat="server" 
     ConnectionString="<%$ ConnectionStrings:contr1 %>" 
     InsertCommand="Insert into OBH([Tech Name], [Total Minutes], [Off Bench Time], 
[Submitted By], Comments, Date)
Select [Tech Name] = @TN, [Total Minutes] = (SELECT [Total Minutes] FROM Total_Tech_Minutes
WHERE ([Employee Name] = @TN)), [Off Bench Time] = @OBT, [Submitted By] = @SB,
Comments = @Comments, Date = (Select Convert(Date, Getdate()))" 
     ProviderName="<%$ ConnectionStrings:contr1.ProviderName %>" 
     CancelSelectOnNullParameter="False" DataSourceMode="DataReader">
   <InsertParameters>
       <asp:ControlParameter ControlID="DropDownList1" Name="TN" 
PropertyName="SelectedValue" />
       <asp:ControlParameter ControlID="TextBox1" Name="OBT" PropertyName="Text" />

                        

这是Home.aspx.cs标记,它已在主页上显示用户名,但我似乎无法将其转换为插入参数格式:

namespace User_Login_CS
{
    public partial class Home : System.Web.UI.Page
    {
         protected void Page_Load(object sender, EventArgs e)
         {
             string SB;

             if (!this.Page.User.Identity.IsAuthenticated)
             {
                   FormsAuthentication.RedirectToLoginPage();
             }
             else
             {
                  SB = Page.User.Identity.Name; 
             }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlDataSource3.Insert();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我明白了。

在home.aspx.cs上,我有以下代码:

using System;
using System.Web.UI;
using System.Web.Security;

namespace User_Login_CS
{
public partial class Home : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.User.Identity.IsAuthenticated)
        {
            FormsAuthentication.RedirectToLoginPage();
        }
        else
        {
        SqlDataSource3.InsertParameters["SB"].DefaultValue = Page.User.Identity.Name;

}

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlDataSource3.Insert();
    }

}
}

在Home.aspx页面上,我有以下代码:

<asp:SqlDataSource ID="SqlDataSource3" runat="server" 
ConnectionString="<%$ ConnectionStrings:contr1 %>" 

InsertCommand="Insert into OBH([Tech Name],[Total Minutes],[Off Bench Time],
[Submitted By],Comments,Date)

Select [Tech Name]=@TN,[Total Minutes]=(SELECT [Total Minutes] FROM Total_Tech_Minutes
WHERE ([Employee Name] = @TN)),[Off Bench Time]=@OBT,[Submitted By]=@SB,
Comments=@Comments,Date=(Select Convert(Date, Getdate()))" 
ProviderName="<%$ ConnectionStrings:contr1.ProviderName %>" 
CancelSelectOnNullParameter="False" DataSourceMode="DataReader">

<InsertParameters>

        <asp:ControlParameter ControlID="DropDownList1" Name="TN" PropertyName="SelectedValue" />
        <asp:ControlParameter ControlID="TextBox1" Name="OBT" PropertyName="Text" />
        <asp:Parameter Name="SB" Type="String" />
        <asp:ControlParameter ControlID="TextBox2" Name="Comments" PropertyName="Text" />
    </InsertParameters>
</asp:SqlDataSource>


</form>