Setting Formview Datasource Parameter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brad Baker

    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(objec t sender, EventArgs e)
    {
    SqlParameter param = new SqlParameter();
    param.Parameter Name = "@department_id ";
    param.Value = "e62bbc7d623f44 a68e101cba90e83 9s3";
    formview_dataso urce.SelectPara meters.Add(para m);
    }

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

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

    Thanks,
    Brad


  • Walter Wang [MSFT]

    #2
    RE: Setting Formview Datasource Parameter

    Hi Brad,

    You can simply use:

    formview_dataso urce.SelectPara meters.Add("@de partment_id",
    "e62bbc7d623f44 a68e101cba90e83 9s3");

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


    Regards,
    Walter Wang (wawang@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.

    Comment

    • Brad Baker

      #3
      Re: Setting Formview Datasource Parameter

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

      Exception Details: System.Data.Sql Client.SqlExcep tion: 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]" <wawang@online. microsoft.comwr ote in message
      news:1zo7fSQnHH A.1428@TK2MSFTN GHUB02.phx.gbl. ..
      Hi Brad,
      >
      You can simply use:
      >
      formview_dataso urce.SelectPara meters.Add("@de partment_id",
      "e62bbc7d623f44 a68e101cba90e83 9s3");
      >
      Please feel free to let me know if there's anything else unclear.
      >
      >
      Regards,
      Walter Wang (wawang@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.
      >

      Comment

      • Walter Wang [MSFT]

        #4
        Re: Setting Formview Datasource Parameter

        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 SqlDataSourceSe lectingEventArg s
        argument:

        <asp:SqlDataSou rce ID="SqlDataSour ce1" runat="server"
        ConnectionStrin g="<%$ ConnectionStrin gs:pubsConnecti onString %>"
        SelectCommand=" SELECT [fname], [lname] FROM [employee] WHERE
        ([job_id] = @job_id)" OnSelecting="Sq lDataSource1_Se lecting">
        <SelectParamete rs>
        <asp:Paramete r Name="job_id" Type="Int16" />
        </SelectParameter s>
        </asp:SqlDataSour ce>


        protected void SqlDataSource1_ Selecting(objec t sender,
        SqlDataSourceSe lectingEventArg s e)
        {
        e.Command.Param eters["@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 (wawang@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.

        Comment

        • Brad Baker

          #5
          Re: Setting Formview Datasource Parameter

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

          Thanks Again,
          Brad


          "Walter Wang [MSFT]" <wawang@online. microsoft.comwr ote in message
          news:outP5DbnHH A.5420@TK2MSFTN GHUB02.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 SqlDataSourceSe lectingEventArg s
          argument:
          >
          <asp:SqlDataSou rce ID="SqlDataSour ce1" runat="server"
          ConnectionStrin g="<%$ ConnectionStrin gs:pubsConnecti onString %>"
          SelectCommand=" SELECT [fname], [lname] FROM [employee] WHERE
          ([job_id] = @job_id)" OnSelecting="Sq lDataSource1_Se lecting">
          <SelectParamete rs>
          <asp:Paramete r Name="job_id" Type="Int16" />
          </SelectParameter s>
          </asp:SqlDataSour ce>
          >
          >
          protected void SqlDataSource1_ Selecting(objec t sender,
          SqlDataSourceSe lectingEventArg s e)
          {
          e.Command.Param eters["@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 (wawang@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.
          >

          Comment

          Working...