1

Need help! I can't get around why this doesn't work.

Page:

<asp:SqlDataSource ID="SelectUserInfo" runat="server" ConnectionString="<%$ ConnectionStrings:TradeRelay %>" SelectCommand="admin_userinfo" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter Name="suser" Type="String" DefaultValue="Anonymous" />
</SelectParameters>
</asp:SqlDataSource>

CodeBehind:

protected void Page_Init(object sender, EventArgs e)
{
    SelectUserInfo.SelectParameters["suser"].DefaultValue = User.Identity.Name;
}

Error:

Could not find control 'suserParam' in ControlParameter 'suser'

UPD:

ControlID="suserParam"

was wrong! Thanks!

master-lame-master
  • 3,101
  • 2
  • 30
  • 47
  • You're instructing the `ControlParameter` to find its value on a control with the ID = `suserParam` - and that's what it's complaining about - it cannot find that control you've defined here.... – marc_s Jan 08 '13 at 06:26

3 Answers3

2

I suggest to add Hiddenfield control and assign the User.Identity.Name to it then make the SelectUserInfo get the parameter value from the Hiddenfield control

by the way, in your code i didn't find any control with the name suserParam

<asp:HiddenField runat="server" ID="suserParam"/>
<asp:SqlDataSource ID="SelectUserInfo" runat="server" ConnectionString="<%$ ConnectionStrings:TradeRelay %>" SelectCommand="admin_userinfo" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="suserParam" Name="suser" Type="String" DefaultValue="Anonymous" />
</SelectParameters>
</asp:SqlDataSource>

this is the code behind

protected void Page_Load(object sender, EventArgs e)
{
    suserParam.value = User.Identity.Name;
}
Nour Berro
  • 550
  • 5
  • 14
2

Your are using ControlParameter which extracts the parameter value from the control existing on the page. So in this case, it is trying to find control with id suserParam and raising error as it is unable to find it.

Try using plain parameter (asp:Parameter) instead of ControlParameter. Yet another alternative is using SessionParameter or writing your own custom parameter (see this SO question: How to utilize ASP.NET current user name in SqlParameter without code-behind)

Community
  • 1
  • 1
VinayC
  • 47,395
  • 5
  • 59
  • 72
-1

You can include it directly as a standard Parameter:

<asp:Parameter Name="suserParam" Type="String" DefaultValue="<%=User.Identity.Name %>" />
M Bowen
  • 20
  • 2