Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I`m trying to fill a gridview from a SalDataSource from code behind. I want to filter the results using 3 text boxes: ScheduerName, Username and Summary. My wish was to keep the aspx page as simple as possible and create the filter parameters from code behind. This code works only if i have one single parameter (example:SchedulerName). I did some research and found that one of the issue were that ConvertEmptyStringToNull was set to false so I tried to set it true for all parameters but with no success.

protected void Page_Load(object sender, EventArgs e)
{
    string connectionString = "Data Source=WIN-KMH6OBJU9JD\\SQL2008;Initial Catalog=TimeRecorder;User ID=username;Password=password";
    SqlDataSource1.ConnectionString = connectionString;
    SqlDataSource1.SelectCommand = "SELECT ScheduleName, Summary,RecursiveTaskID, Priority_RefID, Username, DATEDIFF(DD,lmdf,GETDATE()) AS lmdf FROM vw_General_Tasks";
    SqlDataSource1.FilterExpression = "  (ScheduleName LIKE '%{0}%' or '{0}' ='-1') AND (Username LIKE '%{1}%' or '{1}' ='-1') AND (Summary LIKE '%{2}%' or '{2}' ='-1')";

    SqlDataSource1.FilterParameters.Add(new System.Web.UI.WebControls.ControlParameter("ScheduleName","txtScheduler"));
    SqlDataSource1.FilterParameters.Add(new System.Web.UI.WebControls.ControlParameter("Username", "txtUsername"));
    SqlDataSource1.FilterParameters.Add(new System.Web.UI.WebControls.ControlParameter("Summary", "txtSummary"));

    SqlDataSource1.FilterParameters[0].ConvertEmptyStringToNull = true;
    SqlDataSource1.FilterParameters[1].ConvertEmptyStringToNull = true;
    SqlDataSource1.FilterParameters[2].ConvertEmptyStringToNull = true;    
}

WebForm2.aspx

<form id="form1" runat="server">

        <p>
            Summary:
            <asp:TextBox ID="txtSummary" runat="server" Width="96px" AutoPostBack="true"></asp:TextBox>
        </p>
        <p>
            Scheduler:
            <asp:TextBox ID="txtScheduler" runat="server" Width="96px"></asp:TextBox>
        </p>
        <p>
            Username:
            <asp:TextBox ID="txtUsername" runat="server" Width="96px"></asp:TextBox>
        </p>
        <p>
            <asp:Button runat="server" Text="Filter" ID="btnFilter"></asp:Button>
        </p>


        <asp:SqlDataSource 
            ID="SqlDataSource1" 
            runat="server">
        </asp:SqlDataSource>

        <p>
            <asp:GridView
                ID="GridView1"
                runat="server"
                DataSourceID="SqlDataSource1"
                AutoGenerateColumns="False">
                <Columns>
                    <asp:BoundField DataField="RecursiveTaskID" HeaderText="RecursiveTaskID" SortExpression="RecursiveTaskID" />

                    <asp:BoundField DataField="Summary" HeaderText="Summary" SortExpression="Summary" />

                    <asp:BoundField DataField="ScheduleName" HeaderText="Scheduler" ReadOnly="True" SortExpression="ScheduleName" />

                    <asp:BoundField DataField="Username" HeaderText="Username" HtmlEncode="False" SortExpression="Username" />

                    <asp:BoundField DataField="lmdf" HeaderText="Last Modified" HtmlEncode="False" SortExpression="lmdf" />

                    <asp:BoundField DataField="Priority_RefID" HeaderText="Prio" HtmlEncode="False" SortExpression="Priority_RefID" />
                </Columns>
            </asp:GridView>
        </p>

    </form>
share|improve this question

1 Answer 1

up vote 0 down vote accepted

I'm using a different table but you get the idea:

protected void Page_Load(object sender, EventArgs e)
{
    TextBox1.Text = "A";
    TextBox2.Text = "M";

    SqlDataSource1.FilterExpression = "ContactName like '{0}%' and City like '{1}%'";

    SqlDataSource1.FilterParameters.Add(new ControlParameter("ContactName", "TextBox1", "Text"));
    SqlDataSource1.FilterParameters.Add(new ControlParameter("City", "TextBox2", "Text"));
}

It seems what is missing in your code is the ControlParameter.PropertyName. In the case of a TextBox it would be Text.

Result:

enter image description here

So, in your case you have to do this:

SqlDataSource1.FilterParameters.Add(new System.Web.UI.WebControls.ControlParameter("ScheduleName","txtScheduler", "Text"));
SqlDataSource1.FilterParameters.Add(new System.Web.UI.WebControls.ControlParameter("Username", "txtUsername", "Text"));
SqlDataSource1.FilterParameters.Add(new System.Web.UI.WebControls.ControlParameter("Summary", "txtSummary", "Text"));
share|improve this answer
    
Sorry for the delay, it seems to be working now. Thanks! –  Cosmin Aug 11 at 8:52
    
No problem, thank you! –  jstreet Aug 11 at 11:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.