SqlDataSource.SelectParameters method

Last post 05-04-2008 6:39 AM by matan_hila. 8 replies.

Sort Posts:

  • SqlDataSource.SelectParameters method

    04-27-2008, 11:23 AM
    • Loading...
    • matan_hila
    • Joined on 03-23-2008, 7:20 AM
    • Posts 9

    Hi,

     I am using a grid view with parameter defined in dropdown list. the asp code is:

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Pubs %>"

    SelectCommand="SELECT [au_id], [au_lname], [au_fname], [state] FROM [authors] WHERE ([state] = @state)"

    DeleteCommand="DELETE FROM [authors] WHERE [au_id] = @au_id">

    <SelectParameters>

    <asp:ControlParameter ControlID="DropDownList1" Name="state" PropertyName="SelectedValue"

    Type="String" />

    </SelectParameters>

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Pubs %>"

    SelectCommand="SELECT [au_id], [au_lname], [au_fname], [state] FROM [authors] WHERE ([state] = @state)"

    DeleteCommand="DELETE FROM [authors] WHERE [au_id] = @au_id">

    <SelectParameters>

    <asp:ControlParameter ControlID="DropDownList1" Name="state" PropertyName="SelectedValue"

    Type="String" />

    </SelectParameters>

    </asp:SqlDataSource>

    </asp:SqlDataSource>

     

    I would like to to set the parameter assignment in my VB code in Page_Load methodWhen i am using:
    SqlDataSource1.SelectParameters("state").DefaultValue = DropDownList1.Text

    SqlDataSource1.SelectCommand = "SELECT [au_id], [au_lname], [au_fname], [state] FROM [authors] WHERE ([state] = @state)"

    I get error message saying: Object reference not set to an instance of an object.

    Any ideas how I can set the select parameters (in my VB code) to use the dropdown list I have in my page?

    Thanks,

     Uzi

  • Re: SqlDataSource.SelectParameters method

    04-27-2008, 11:54 AM
    • Loading...
    • SrDhUS
    • Joined on 12-28-2004, 10:17 AM
    • IL,Chicago
    • Posts 108

    Check this video: http://www.asp.net/learn/videos/video-49.aspx. This explains how you should be using a Drop Down List and a GridView to filter data from a datasource (in this case a SQL Server table)

    Please remember to click “Mark as Answer” on the post that helps you
  • Re: SqlDataSource.SelectParameters method

    04-27-2008, 11:55 AM

    Hi Friend,

    in which line did you get that error ?

    could you show the line...?!

    With Friendly,
    Thirumaran

    Please remember to click "Mark as Answer" on this post if it helped you
  • Re: SqlDataSource.SelectParameters method

    04-28-2008, 2:40 AM
    • Loading...
    • matan_hila
    • Joined on 03-23-2008, 7:20 AM
    • Posts 9

    Greetings friend,

    I get it in the line:

    SqlDataSource1.SelectParameters("state").DefaultValue = DropDownList1.Text

    Thanks,

    Uzi 

     

  • Re: SqlDataSource.SelectParameters method

    04-28-2008, 2:55 AM

    I don' have an experience in sqldatasource. but i will give an idea.

    Here you are getting the error "object reference not set to instance of an object"

    that means you are trying to access an object whose value is null.

    here , "SqlDataSource1.SelectParameters("state")" is returning "null" . so it is giving this error.

    try this, SqlDataSource1.SelectParameters("@state") instead of SqlDataSource1.SelectParameters("state")

    still if it is not giving the results. debug it. set a break point there,

    check the values of "sqldatasource1.selectparameters" ---------> it will show the parameter collection, show that properties, it will show the count of those parameters and values in that collection. see those. you will get an idea.

  • Re: SqlDataSource.SelectParameters method

    04-28-2008, 6:04 AM
    • Loading...
    • matan_hila
    • Joined on 03-23-2008, 7:20 AM
    • Posts 9

    Hi,

    Still the same error....Tongue Tied 

    Thansk for you help!

    Uzi 

     

     

  • Re: SqlDataSource.SelectParameters method

    05-02-2008, 3:58 AM
    Answer
    SqlDataSource1.SelectParameters("state").DefaultValue = DropDownList1.Text

    SqlDataSource1.SelectCommand = "SELECT [au_id], [au_lname], [au_fname], [state] FROM [authors] WHERE ([state] = @state)"

    Based on my knowledge, I don't think it would work. Since you have already configured your sqldatasource in your page markup, and have set "@state" to take the value from your DropdownList1.Selected Value, (by this:

    <asp:ControlParameter ControlID="DropDownList1" Name="state" PropertyName="SelectedValue" Type="String" /> ), the parameter @state's value would be determined at run time and would be assigned value from your dropdwonlist.  You cannot change its value by setting its defaultvalue property.

    Any ideas how I can set the select parameters (in my VB code) to use the dropdown list I have in my page?

    I'm not a little confused on this, since I think your datasrouce.selectParameters["state"] has already used the dropdown list in your page. You don't have to write any more code in your code behind. Just bind your datasource to a displaying control, anything would be fine.

    Hope my suggestion helps

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
  • Re: SqlDataSource.SelectParameters method

    05-02-2008, 6:05 AM
    Answer
    • Loading...
    • ShivaKarthik
    • Joined on 04-15-2008, 1:11 PM
    • Hyderabad
    • Posts 218

    The problem is here.

    Since you already assigned value to ur state once in sqldatasource

    And again ur assigning value to it so it is giving an error.

    The solution is you declare either in the Menu based-Wizard or declare locally from scratch but don't perform operations both at the same time.

    And also the method you declared is wrong.It should be as

    SqlDataSource1.SelectParameters.Add("@state", DropDownList1.Text);

    ok any way i think thi ssolves your problem

    Mark As Answer if this solves your Problem

    Further Queries Recommended

    Happy CodingWink

    Regards,
    B.ShivaKarthik
    Software Engineer
    Hyderabad,India
  • Re: SqlDataSource.SelectParameters method

    05-04-2008, 6:39 AM
    • Loading...
    • matan_hila
    • Joined on 03-23-2008, 7:20 AM
    • Posts 9

    Hi,

    Thanks for the info. I manamged to solve it finally Idea!!!!!!!

    The solution is to add the new parameter:

    SqlDataSource1.SelectParameters.Add("state", "Value")

    And then to use it in the select parameters statement

    SqlDataSource1.SelectParameters("state").DefaultValue = DropDownList1.Text

    Many thnks for your help. 

     

     

     

     

Page 1 of 1 (9 items)
Microsoft Communities
Page view counter