Passing Windows User Id as parameter to SQLDataSource gives databinding exception

女生的网名这么多〃 提交于 2019-12-13 21:32:59

问题


First question is, Am I on the right path?.ıs there a better way to pass it as parameter? If I am on the right path, please show me how can I solve the below error.

The following solution does not help me with this problem: HTTPContext.Current.User.Identity.Name not working inside a control?

My code:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:MyDbConn %>" 
    SelectCommand="SELECT id, Bookname, RequestType, Requestor, RequestDate FROM Requests WHERE (Requestor LIKE '%' + @Requestor + '%')">
    <SelectParameters>
        <asp:Parameter DefaultValue= "<%# HttpContext.Current.User.Identity.Name.Split('\\')[1] %>" Name="Requestor" />
    </SelectParameters>
</asp:SqlDataSource>

Gives the below error:


回答1:


try this

public Requests()
{
   this.Init += (_o, _e) => 
   {
       this.SqlDataSource1.Selecting += (o, e) =>
       {
           (o as SqlDataSourceView)
             .SelectParameters["Requestor"].DefaultValue = 
                      HttpContext.Current.User.Identity.Name;
       }
   }
}

or

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            SqlDataSource1.SelectParameters["Requestor"].DefaultValue = 
                          HttpContext.Current.User.Identity.Name;
        }
    }



回答2:


Try this i didn't Check It

replace the # to =

<asp:Parameter DefaultValue= "<%= HttpContext.Current.User.Identity.Name.Split('\\')[1] %>" Name="Requestor" />



回答3:


well.... it says u can't use the <%# %> (databinding expression) on this control.

I'm not familiar with this control, but you could try setting te property value at the beginning of the page life cycle.



来源:https://stackoverflow.com/questions/9863899/passing-windows-user-id-as-parameter-to-sqldatasource-gives-databinding-exceptio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!