Dynamic SQL Parameters

27 views
Skip to first unread message

BigJ

unread,
Nov 10, 2008, 10:35:13 PM11/10/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting

In my sqlDataConnection I have :

//=============================================================
<asp:SqlDataSource
id="srcProfileView"
ConnectionString="<%$ ConnectionStrings:webBlog %>"
SelectCommandType="StoredProcedure"
SelectCommand="profileInfo"
Runat="server">

<SelectParameters>
<asp:Parameter Name="UserId" Type="String" Size="100"
DefaultValue="Untitled"/>
</SelectParameters>
</asp:SqlDataSource>
//=============================================================


However, I want to make the value of "UserId" dynamic with what's
being passed in from the URL, I took out the <selectparameter> tag and
in my Page_load I did the following:


//=============================================================
void Page_Load()
{
String UserId = Page.Request.Params.Get("UserId");
srcProfileView.SelectParameters.Add(new
Parameter(@UserId,TypeCode.String,UserId));
}
//=============================================================


From what I understand this should Add a new parameter to the
sqlDataConnection control with the name "UserId" and whatever the
value of UserId is. However I'm getting the error that my stored
procedure expects a value for @UserId. Any insight?

Thanks

Cerebrus

unread,
Nov 11, 2008, 12:38:50 AM11/11/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
1. It appears that your parameter value is equivalent to something
passed in the QueryString or through the Form collection. In these
cases, it would be more appropriate for you to simply use a
QueryStringParameter or FormParameter. This would obviate the need for
setting your Parameter on Page load.

2. Since you are interested only in the SelectCommand, the
SqlDataSource_Selecting event is the place to set the value of the
Parameter. Page_Load would not be the correct place, IMHO.

3. You should not be adding a new Parameter, but should instead access
the declared Parameter and set it's value, or else it invalidates the
purpose of using the Declarative model.

BigJ

unread,
Nov 12, 2008, 5:26:50 PM11/12/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Thanks,

So being that I leave

<SelectParameters>
<asp:Parameter Name="UserId" Type="String" Size="100"
DefaultValue="untitled"/>
</SelectParameters>

in the code, and in addition use OnSelecting as follows:

<asp:SqlDataSource
id="srcProfileView"
ConnectionString="<%$ ConnectionStrings:webBlog %>"
SelectCommandType="StoredProcedure"
SelectCommand="profileInfo"
Runat="server"
OnSelecting="get_UserId">

what is the correct syntax in get_UserId to set the value of the
select parameter field "UserId"? The following is what I have, and it
is erroring out:

void get_UserId() {
String UserVal = Page.Request.Params.Get("UserId");
srcProfileView.SelectParameters.Add(new Parameter
(@UserId,TypeCode.String,UserVal));
}

Thanks

Jon

BigJ

unread,
Nov 12, 2008, 5:36:44 PM11/12/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Also...I used QueryStringParameter and it worked fine...but i was just
curious as to how this could be done using C#. Thanks.

Cerebrus

unread,
Nov 13, 2008, 12:30:31 AM11/13/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Here ya go ! ;-)
---
protected void srcProfileView_Selecting(object sender,
SqlDataSourceSelectingEventArgs e)
{
string userVal = Request.Params("UserId");
e.Command.Parameters("@UserId").Value = userVal;
}
---
> > > > Thanks- Hide quoted text -
>
> - Show quoted text -
Reply all
Reply to author
Forward
0 new messages