Setting Formview Datasource Parameter

Discussion in 'ASP .Net' started by Brad Baker, May 23, 2007.

  1. Brad Baker

    Brad Baker Guest

    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
    Brad Baker, May 23, 2007
    #1
    1. Advertising

  2. 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 (, 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.
    Walter Wang [MSFT], May 23, 2007
    #2
    1. Advertising

  3. Brad Baker

    Brad Baker Guest

    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]" <> wrote in message
    news:...
    > 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 (, 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.
    >
    Brad Baker, May 23, 2007
    #3
  4. 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 (, 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.
    Walter Wang [MSFT], May 24, 2007
    #4
  5. Brad Baker

    Brad Baker Guest

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

    Thanks Again,
    Brad


    "Walter Wang [MSFT]" <> wrote in message
    news:eek:...
    > 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 (, 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.
    >
    Brad Baker, May 24, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nate Hekman

    asp.net 2.0, datasource/formview question

    Nate Hekman, Dec 28, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    472
    bruce barker
    Dec 29, 2004
  2. Rabbit
    Replies:
    0
    Views:
    1,708
    Rabbit
    Mar 20, 2006
  3. sck10

    FormView datasource (asp.net 2.0)

    sck10, Mar 29, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    5,133
    Steven Cheng[MSFT]
    Mar 30, 2006
  4. Mike Gaab

    formview and datasource as collection

    Mike Gaab, Apr 2, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    2,063
    Mike Gaab
    Apr 3, 2006
  5. Jason
    Replies:
    0
    Views:
    201
    Jason
    Oct 24, 2006
Loading...

Share This Page