I'm trying to set the SelectParameters of SqlDataSource from the code behind.
This is my query: SELECT * FROM [UploadSessions] WHERE ([OwnerID] = @OwnerID) And I need to set the value of the @OwnerID at the code behind since it is stored in an object.
Check what the actual valud of the ProviderUserKey is and see that the value actually makes sense. For example, maybe that OwnerID doesn't return any results because there simply aren't any. Try manually setting the value to be a value that you know returns
results and see what happens.
Bar
Member
20 Points
4 Posts
Setting the SelectParameters of SqlDataSource
Dec 30, 2005 03:43 PM|LINK
Hi All,
I'm trying to set the SelectParameters of SqlDataSource from the code behind.
This is my query: SELECT * FROM [UploadSessions] WHERE ([OwnerID] = @OwnerID)
And I need to set the value of the @OwnerID at the code behind since it is stored in an object.
How can I do it??
Thanks in advance
Bar
Eilon
Contributor
5753 Points
976 Posts
Microsoft
Re: Setting the SelectParameters of SqlDataSource
Dec 30, 2005 06:42 PM|LINK
In some event (for example Page_Load, or SqlDataSource.Selecting) add this code:
SqlDataSource1.SelectParameters["OwnerID"].DefaultValue = "some value";
Make sure you add a declarative parameter to the SelectParameters collection, too:
<asp:SqlDataSource ... >
<SelectParameters>
<asp:Parameter Name="OwnerID" />
</SelectParameters>
</asp:SqlDataSource>
Thanks,
Eilon
Bar
Member
20 Points
4 Posts
Re: Setting the SelectParameters of SqlDataSource
Dec 30, 2005 08:07 PM|LINK
Thanks Eilon,
This is what I tried to do at the first place, but it's looks like its not working.
When i debug it and place a break point on:
SqlDataSource1.SelectParameters["OwnerID"].DefaultValue = "some value";
The debugger is stopping on this line but I still do not get any results, any ideas why?
Here is my code:
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
SqlDataSource1.SelectParameters["OwnerID"].DefaultValue = Membership.GetUser(true).ProviderUserKey.ToString();
}
Thanks Bar
Eilon
Contributor
5753 Points
976 Posts
Microsoft
Re: Setting the SelectParameters of SqlDataSource
Dec 30, 2005 09:36 PM|LINK
Check what the actual valud of the ProviderUserKey is and see that the value actually makes sense. For example, maybe that OwnerID doesn't return any results because there simply aren't any. Try manually setting the value to be a value that you know returns results and see what happens.
Thanks,
Eilon
USFbobFL
Member
195 Points
39 Posts
Re: Setting the SelectParameters of SqlDataSource
Apr 26, 2006 08:07 PM|LINK
Here is how I did it in VS 2005 (VB.NET) in the Page_Load event.
SqlDataSource1.SelectParameters.Item("UserName").DefaultValue = My.User.Name
----------------- exerpts from SqlDataSource
WHERE (CustRequests.UserName = @UserName)
<asp:Parameter Name="UserName" Type="String" />
klpatil
Participant
1290 Points
245 Posts
Re: Setting the SelectParameters of SqlDataSource
Apr 23, 2009 07:18 AM|LINK
Hi All,
As per my knowledge you can achieve it using following 2 ways:
1. Set Select Parameters value from Code-behind like as shown below:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"> <SelectParameters> <asp:Parameter Name="UserID" Type="Int64" /> </SelectParameters> </asp:SqlDataSource>In Page_Load event or after binding your code behind variable do set the select parameters value as following:
//set SqlDataSource parameters value
OR2. Use Control Parameters as shown below:
<asp:SqlDataSource ID="SqlDataSource1" runat="server">
<SelectParameters>
<asp:ControlParameter Name="UserID" ControlID="hdnUserID" PropertyName="Value" />
</SelectParameters>
</asp:SqlDataSource>
<%--Hidden field to store UserID--%>
<asp:HiddenField ID="hdnUserID" runat="server" />
In Page_Load event or after binding your code behind variable do set the select parameters value as following:
HTH
-Kiran
For more solution like this my blog is here
acole86
Member
14 Points
2 Posts
Re: Setting the SelectParameters of SqlDataSource
Jun 01, 2009 06:31 PM|LINK
I know this is an old topic, but for those still searching for this... Here is the best method I found for binding parameters...
First create an event handler for your datasource OnSelecting Event like so:
Then, set the values using the event arguments:
limno
All-Star
117336 Points
8003 Posts
Moderator
MVP
Re: Setting the SelectParameters of SqlDataSource
Jun 01, 2009 09:31 PM|LINK
The event arg should be:
protected void ds_Selecting(object sender, SqlDataSourceSelectingEventArgs e){
//e.Command.Parameters[0].Value=User.Identity.Name;//Label1.Text = Membership.GetUser().ProviderUserKey.ToString()
}
Format your SQL query with instant sql formatter:
http://www.dpriver.com/pp/sqlformat.htm