0

I have a sqldatasource, in select command i have @status parameter. The parameter take the value from the textbox at runtime.

 <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
            SelectCommand="SELECT * FROM [project_details] WHERE ([status] = @status)" 
            FilterExpression="title='{4}'" 
            ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
            EnableCaching="True">
 <SelectParameters>
                <asp:ControlParameter ControlID="TextBox1" Name="status" PropertyName="Text" ConvertEmptyStringToNull="false"
                    Type="String" />
            </SelectParameters>

        </asp:SqlDataSource>

my problem is that when i run the page without entring the parameter in text box sqldatasource is not returing any row.

4 Answers 4

1

Looking at the documentation on MSDN, you have to alter how you have setup the SqlDataSource.

Try this:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        SelectCommand="SELECT * FROM [project_details]" 
        FilterExpression="title='{0}'" 
        ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
        EnableCaching="True">
    <FilterParameters>
            <asp:ControlParameter ControlID="TextBox1" Name="status" PropertyName="Text" ConvertEmptyStringToNull="false" Type="String" />
    </FilterParameters>
</asp:SqlDataSource>

I have removed the Where clause from the query as this will be applied by the filter expression. Also, I have changed the filter expression from title='{4}' to title='{0}'. The documentation states that the number is a placeholder to an item in the FilterParameters collection.

Updated I have also changed the SelectParameters to FilterParameters

Update 2

I have created a working example to finish of this answer. This will filter the Title column using the text from the text box. If this text box is empty it will return all the rows from the table (a scary thought but OK for this example). It is querying the AdventureWorks database for which I set a connection string called AWorks.

<asp:SqlDataSource ID="SqlDataSource1"
    ConnectionString="<%$ ConnectionStrings:AWorks %>"
    SelectCommand="SELECT ContactId, Title, FirstName, LastName FROM Person.Contact"
    FilterExpression="Title='{0}'"
    runat="server">
 <FilterParameters>
    <asp:ControlParameter Name="Title" ControlID="txtTitle" PropertyName="Text" />
 </FilterParameters> 
</asp:SqlDataSource>

<asp:TextBox runat="server" Id="txtTitle"></asp:TextBox>
<asp:Button runat="server" UseSubmitBehavior="true" Text="Submit" />

<asp:GridView 
    DataSourceID="SqlDataSource1"
    AutoGenerateColumns="false"
    runat="server">
<Columns>
    <asp:BoundField Visible="false" DataField="ContactId"></asp:BoundField>
    <asp:BoundField Visible="true" DataField="Title"></asp:BoundField>
    <asp:BoundField Visible="true" DataField="FirstName"></asp:BoundField>
    <asp:BoundField Visible="true" DataField="LastName"></asp:BoundField>
</Columns>
</asp:GridView>
Sign up to request clarification or add additional context in comments.

4 Comments

hey thanks but here i forgot to mention my filter parameter.
Good point. I have updated the answer and change the SelectParameters to a FilterParameters collection
i totally agree with you Keith .it should work .but i dont know why even after adding your code it is not working
conclusion filter expression sucks i am working with where statement to solve my problem
0

try a condition like this:

(@Status is null or @Status ='' Or Status = @Status)

Comments

0

set ConvertEmptyStringToNull="true" and then try....

Comments

-1

Add CancelSelectOnNullParameter="false" to your SqlDataSource.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.