Setting Formview Datasource Parameter

B

Brad Baker

I'm trying to set a formview datasource parameter dynamically on page_load
using the following code:

public void Page_Load(object sender, EventArgs e)
{
SqlParameter param = new SqlParameter();
param.ParameterName = "@department_id";
param.Value = "e62bbc7d623f44a68e101cba90e839s3";
formview_datasource.SelectParameters.Add(param);
}

But I'm getting the following error: Compiler Error Message: CS1502: The
best overloaded method match for
'System.Web.UI.WebControls.ParameterCollection.Add(System.Web.UI.WebControls.Parameter)'
has some invalid arguments

Am I going about this wrong? What is the best way to dynamically set a
datasource parameter?

Thanks,
Brad
 
W

Walter Wang [MSFT]

Hi Brad,

You can simply use:

formview_datasource.SelectParameters.Add("@department_id",
"e62bbc7d623f44a68e101cba90e839s3");

Please feel free to let me know if there's anything else unclear.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
B

Brad Baker

The code you provided below makes sense to me but it doesn't work either.
Now I get:

Exception Details: System.Data.SqlClient.SqlException: Must declare the
variable '@department_id'.

I am calling this code from Page_Load in a custom web control. Does
Page_Load not get called in web controls?

Thanks
Brad
 
W

Walter Wang [MSFT]

Hi Brad,

I'm very sorry about my previous reply, I was replying too quick.

To set a SelectCommand's parameter for a SqlDataSource control, you should
handle its Selecting event and use the SqlDataSourceSelectingEventArgs
argument:

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"
SelectCommand="SELECT [fname], [lname] FROM [employee] WHERE
([job_id] = @job_id)" OnSelecting="SqlDataSource1_Selecting">
<SelectParameters>
<asp:parameter Name="job_id" Type="Int16" />
</SelectParameters>
</asp:SqlDataSource>


protected void SqlDataSource1_Selecting(object sender,
SqlDataSourceSelectingEventArgs e)
{
e.Command.Parameters["@job_id"].Value = 11;
}



I'm using the pubs sqlserver 2000 database for test.

Please try this on your side and let me know the results. Thanks.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
B

Brad Baker

Thank you so much! :) I was struggling forever trying to figure out how to
acheive this.

Thanks Again,
Brad
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top