Programmatically set FilterExpression and FilterParameters of SqlDataSource

Discussion in 'ASP .Net' started by fuchs.markus@gmail.com, Aug 25, 2005.

  1. Guest

    Hello,
    on my page, I have a DetailsViewControl connected to a SqlDataSource.
    Now when I dynamically set the FilterExpression and FilterParameters -
    for example in a OnClick-EventHandler of a button - the DetailsView
    shows the filtered Data, but Deleting and Updating doesn't work
    anymore. The DetailsView changes to EditMode, but after clicking on the
    UpdateButton it shows the old data.
    Am I missing something or is it just not possible or perhaps a beta
    problem?

    Here is my Button-Click-EventHandler:

    protected void Button1_Click(object sender, EventArgs e)
    {
    SqlDataSource1.FilterExpression = "CompanyName LIKE '{0}'";
    SqlDataSource1.FilterParameters.Add("CompanyName", "A*");
    }


    And here my Page-SourceCode:

    <asp:DetailsView ID="DetailsView1" runat="server"
    AllowPaging="True" AutoGenerateRows="False"
    DataKeyNames="CustomerID" DataSourceID="SqlDataSource3"
    Height="50px" Width="125px">
    <Fields>
    <asp:BoundField DataField="CustomerID"
    HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" />
    <asp:BoundField DataField="CompanyName"
    HeaderText="CompanyName" SortExpression="CompanyName" />
    <asp:BoundField DataField="ContactName"
    HeaderText="ContactName" SortExpression="ContactName" />
    <asp:BoundField DataField="ContactTitle"
    HeaderText="ContactTitle" SortExpression="ContactTitle" />
    <asp:BoundField DataField="Address"
    HeaderText="Address" SortExpression="Address" />
    <asp:BoundField DataField="City" HeaderText="City"
    SortExpression="City" />
    <asp:BoundField DataField="Region" HeaderText="Region"
    SortExpression="Region" />
    <asp:BoundField DataField="PostalCode"
    HeaderText="PostalCode" SortExpression="PostalCode" />
    <asp:BoundField DataField="Country"
    HeaderText="Country" SortExpression="Country" />
    <asp:BoundField DataField="Phone" HeaderText="Phone"
    SortExpression="Phone" />
    <asp:BoundField DataField="Fax" HeaderText="Fax"
    SortExpression="Fax" />
    <asp:CommandField ShowDeleteButton="True"
    ShowEditButton="True" ShowInsertButton="True" />
    </Fields>
    </asp:DetailsView>

    <asp:SqlDataSource ID="SqlDataSource3" runat="server"
    ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString5 %>"
    DeleteCommand="DELETE FROM [Customers] WHERE [CustomerID] =
    @original_CustomerID"
    InsertCommand="INSERT INTO [Customers] ([CustomerID],
    [CompanyName], [ContactName], [ContactTitle], [Address], [City],
    [Region], [PostalCode], [Country], [Phone], [Fax]) VALUES (@CustomerID,
    @CompanyName, @ContactName, @ContactTitle, @Address, @City, @Region,
    @PostalCode, @Country, @Phone, @Fax)"
    SelectCommand="SELECT * FROM [Customers]"
    UpdateCommand="UPDATE [Customers] SET [CompanyName] = @CompanyName,
    [ContactName] = @ContactName, [ContactTitle] = @ContactTitle, [Address]
    = @Address, [City] = @City, [Region] = @Region, [PostalCode] =
    @PostalCode, [Country] = @Country, [Phone] = @Phone, [Fax] = @Fax WHERE
    [CustomerID] = @original_CustomerID" OnUpdated="SqlDataSource3_Updated"
    OnUpdating="SqlDataSource3_Updating">
    <DeleteParameters>
    <asp:parameter Name="original_CustomerID" Type="String"
    />
    </DeleteParameters>
    <UpdateParameters>
    <asp:parameter Name="CompanyName" Type="String" />
    <asp:parameter Name="ContactName" Type="String" />
    <asp:parameter Name="ContactTitle" Type="String" />
    <asp:parameter Name="Address" Type="String" />
    <asp:parameter Name="City" Type="String" />
    <asp:parameter Name="Region" Type="String" />
    <asp:parameter Name="PostalCode" Type="String" />
    <asp:parameter Name="Country" Type="String" />
    <asp:parameter Name="Phone" Type="String" />
    <asp:parameter Name="Fax" Type="String" />
    <asp:parameter Name="original_CustomerID" Type="String"
    />
    </UpdateParameters>
    <InsertParameters>
    <asp:parameter Name="CustomerID" Type="String" />
    <asp:parameter Name="CompanyName" Type="String" />
    <asp:parameter Name="ContactName" Type="String" />
    <asp:parameter Name="ContactTitle" Type="String" />
    <asp:parameter Name="Address" Type="String" />
    <asp:parameter Name="City" Type="String" />
    <asp:parameter Name="Region" Type="String" />
    <asp:parameter Name="PostalCode" Type="String" />
    <asp:parameter Name="Country" Type="String" />
    <asp:parameter Name="Phone" Type="String" />
    <asp:parameter Name="Fax" Type="String" />
    </InsertParameters>
    </asp:SqlDataSource>

    Thanks in advance
    Markus Fuchs
     
    , Aug 25, 2005
    #1
    1. Advertising

  2. Markus Guest

    Hi all,
    I've found a solution to my problem: If I leave the FilterParameters
    alone and write keys as well as values in the FilterExpression-String,
    everything works properly.

    MF
     
    Markus, Aug 25, 2005
    #2
    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. Henry Habermacher [MVP Access]

    GridView - Changing FilterExpression of SQLDataSource and Paging

    Henry Habermacher [MVP Access], Jan 20, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    11,047
    kmarchiony
    Jul 31, 2007
  2. Replies:
    0
    Views:
    3,083
  3. lewis
    Replies:
    0
    Views:
    458
    lewis
    Jan 9, 2007
  4. lewis
    Replies:
    0
    Views:
    489
    lewis
    Jan 10, 2007
  5. Mariela Donkova

    Dynamically Add FilterExpression and FilterParameters

    Mariela Donkova, Jan 10, 2006, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    173
    Mariela Donkova
    Jan 10, 2006
Loading...

Share This Page