473,320 Members | 1,744 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Setting Formview Datasource Parameter

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
May 23 '07 #1
4 6975
Hi Brad,

You can simply use:

formview_datasource.SelectParameters.Add("@departm ent_id",
"e62bbc7d623f44a68e101cba90e839s3");

Please feel free to let me know if there's anything else unclear.
Regards,
Walter Wang (wa****@online.microsoft.com, 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.

May 23 '07 #2
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
"Walter Wang [MSFT]" <wa****@online.microsoft.comwrote in message
news:1z**************@TK2MSFTNGHUB02.phx.gbl...
Hi Brad,

You can simply use:

formview_datasource.SelectParameters.Add("@departm ent_id",
"e62bbc7d623f44a68e101cba90e839s3");

Please feel free to let me know if there's anything else unclear.
Regards,
Walter Wang (wa****@online.microsoft.com, 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.

May 23 '07 #3
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 (wa****@online.microsoft.com, 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.

May 24 '07 #4
Thank you so much! :) I was struggling forever trying to figure out how to
acheive this.

Thanks Again,
Brad
"Walter Wang [MSFT]" <wa****@online.microsoft.comwrote in message
news:ou**************@TK2MSFTNGHUB02.phx.gbl...
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 (wa****@online.microsoft.com, 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.

May 24 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: sck10 | last post by:
Hello, I am creating a form for users to enter information about a lab and the members of the lab. I have one form (FormView) that they use to enter information about that lab. The keyvalue is...
2
by: P. Yanzick | last post by:
Hello, I am creating an edit template for a FormView control, changing one of the textboxes to a dropdown box. The dropdown will be populated from a simple table with the primary key, and a...
1
by: sck10 | last post by:
Hello, I have a GridView that has AutoGenerateSelectButton="true". When the "Select" link is clicked, it opens a FormView with the appropriate data. Below is the following SelectParameter that...
3
by: Bas Paap | last post by:
I'm having a problem with formviews and DropDownLists in ASP.NET 2.0. I'm using a formview to insert an order into a database. Part of the order is the customer. Currently, I have a dropdownlist...
5
by: mosscliffe | last post by:
Following Mr Tabors Video on Databinding I implemented the following code In GridViewSelectedIndexChange I coded DetailsView1.pageindex = gridview1.selectedindex This works fine for the...
0
by: Sam | last post by:
I am trying to use a Simple form with 3 fields from SQL NorthWind Database (Order Details Table with 3 Fields. - OrderId, ProductId and Unit Price). The Field Unit Price has a data type of 'Money...
0
by: Sam | last post by:
Folks.. I am trying to use a Simple form with 3 fields from SQL NorthWind Database (Order Details Table with 3 Fields. - OrderId, ProductId and Unit Price). The Field Unit Price has a data...
0
by: DC | last post by:
The problem I'm using the .NET GridView and FormView objects for the first time and im getting the error "An OleDbParameter with ParameterName '@ID' is not contained by this...
0
by: Cirene | last post by:
I have a formview that is calling a stored procedure (thru an sqldatasource) to update 2 tables. When I execute the update it runs fine, with no errors. But the data does NOT change. Any ideas...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.