Filter expression not working properly - asp.net

I am using a GridView and I require the user to be able to filter using 2 controls. One simply filters the type of row - there is a column called action, and the user selects one of the distinct values from the database in a dropdown box, and the gridview only displays the rows with that value in the action column. On it's own this works perfect.
But I am also adding a textbox where the user can type in an ID of either the 'itemID', or the 'parentID'. This works fine even with the previous control.
Problem is, the dropdown box does not work when the textbox is empty (i.e. all ID's are being shown). If I choose a value and click Submit, it just doesn't do anything. Now if I am filtering by ID and then choose a value from the dropdown box, it works perfect.
Here is my filter expression and parameters of the datasource:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="cleared for privacy"
SelectCommand="SELECT * FROM [Audit] WHERE ([source] = #source)" FilterExpression="action like '{0}%' and (itemID like '{1}' or parentID like '{1}')">
<SelectParameters>
<asp:Parameter DefaultValue="LOGISTICS" Name="source" Type="String" />
</SelectParameters>
<FilterParameters>
<asp:ControlParameter Name="action" ControlID="DropDownList1" PropertyName="SelectedValue" />
<asp:ControlParameter Name="legorinvid" ControlID="txtFilter" PropertyName="Text" />
</FilterParameters>
</asp:SqlDataSource>

In my case, replacing the SQL filter expression
LIKE '{1}%' by LIKE '{1}'
made the difference.
The former did not work, the latter did.

Have you tried setting the ConvertEmptyStringToNull="false" field on the control parameter?

Related

ASP.NET SqlDataSource, like on SelectCommand

I'm working on asp.net. I Have a SqlDataSource with a query hardcoded on selectcommand:
<asp:SqlDataSource ID="DataSource1" runat="server" CancelSelectOnNullParameter="False"
ConnectionString="<%$ ConnectionStrings:S.Properties.Settings.ConnectionString %>"
SelectCommand="SELECT * FROM [table]
WHERE ([col1] like Case #col1_param When null Then col1 Else #col1_param End)
and ([col2] like Case #col2_param When null Then col2 Else #col2_param End)"
SelectCommandType="Text">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" Name="col1_param" PropertyName="Text"
Type="String" />
<asp:ControlParameter ControlID="TextBox2" Name="col2_param" PropertyName="Text"
Type="String" />
</SelectParameters>
What I want is that if you enter data on one textbox only, the data will display according with that textbox value only on the where clause. And if no values are placed for neither of the textboxes, the the query executes as if there is no where.
Right now with this code,what happens is if you put on one textbox only no data is displayed. The same if all textboxes are empty.
I don't want to use sql stored procedure.
How can I solve this?
Thanks...
Assuming it passes null when there is no text entered, otherwise you will need to check for the empty string
SelectCommand="SELECT * FROM [table]
WHERE ([col1] like '%#col1_param%' or #col1_param is null)
and ([col2] like '%#col2_param%' or #col2_param is null)"
It sounds like you want your query to optionally search a column.
You can use the format
WHERE #col1_param IS NULL OR [col1] LIKE '%#col1_param%'
to property handle the case where the parameter is not specified.
See my question on the issue for a full answer. Granted it was done as a stored procedure, but the concept will hold the same for your SQLDataSource.

SqlDataSource with DropDownList Control Parameter doesn't read value

I have a drop down list that bound to a SqlDataSource.
I have another drop down list that's bound to a different SqlDataSource.
The second SqlDataSource has the first drop down as a Control Parameter.
I'm trying to do this...
<asp:SqlDataSource ID="sqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM Test WHERE Param = #param;"
CancelSelectOnNullParameter="true">
<SelectParameters>
<asp:ControlParameter ControlID="dropDown1" Name="param"
PropertyName="SelectedValue"
ConvertEmptyStringToNull="true" />
</SelectParameters>
</asp:SqlDataSource>
dropDown1.SelectedValue = "someValue"
dropDown2.DataBind()
but I don't get any results. However, if I set the second SqlDataSource's Control Parameter to a text box, it works. For example, this works:
<asp:ControlParameter ControlID="txt" Name="param"
PropertyName="Text"
ConvertEmptyStringToNull="true" />
txt.Text = "someValue"
dropDown2.DataBind()
Any ideas why this is?
I ended up figuring this one out. The problem was that the drop down was attempting to bind twice, much like the problem in this question.
I used the suggestion made by Joel Etherton, and now it works perfectly. Although I used a hidden control rather than a label.

Control doesn't bind to SqlDataSource on page load with blank parameters

Suppose you have a SQLDataSource that looks like this:
<asp:SqlDataSource ID="sqldsSample" runat="server"
ConnectionString="<%$ ConnectionStrings:myConnectionString %>"
SelectCommand="SELECT [col1], [col2] FROM [tbl] WHERE [col3] = #val) ORDER BY [col1] DESC;">
<SelectParameters>
<asp:Parameter DefaultValue="False" Name="val" Type="Boolean" />
<asp:Parameter DefaultValue="" Name="val2" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
Just assume you have decided you want to declare the 'val2' parameter here and you don't want to bother with adding and/or removing parameters later in the code-behind (say, to change the SelectCommand to do some filtering with some extra criteria).
It will fail without an error - the control will just show up empty.
You have to specify a default value for the parameter even if it's not used. For example, just putting in a space character will work:
<asp:Parameter DefaultValue=" " Name="val2" Type="String" />
Note that it still won't work if you omit the DefaultValue property.
There is also the CancelSelectOnNullParameter parameter on SqlDataSource - you need to set this to false if you expect any of your parameters to be null.
It's not very obvious and it has caught me out a few times!

asp.ControlParamter control id conflict

<asp:ControlParameter ControlID="ddListPlayerPointSystems" Name="profileid" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="ddListCmty" Name="cmty" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="ctl00$MainContent$TabContainer1$TabPanel1$FormView3$pointsTextBox" Name="InsertPts" PropertyName="Text" Type="Decimal" />
I am having trouble understanding why in the first controlparameter i can call the dropdownbox id but not the textboxes id which is pointsTextBox. I am using a master page with an asp ajax tab container with multiple panels. If i take off the "ctl00$MainContent$TabContainer1$TabPanel1$FormView3$" i get a control not found but i dont know why this works for the other two controlparameters
EDIT
So I found a solution to my problem. Thanks to #TheGeekYouNeed and #JamesJ I understand why I would require the longer path name for that particular textbox (the drops were outside of the tabcontainer so the direct name worked). But I found that since I was assigning the value of that textbox via '<%# Bind("name", "{0:n}") %>' I was able to instead just use an asp:Parameter rather than the ControlParameter like so:
"<asp:Parameter Name="name" Type="String" />"
Problem is that i don't quite understand how that all works.
The ControlID for the pointsTextBox is not 'ct100$MainContent$TabContainer..etc... on the server side.
Set the COntrolID in the code behind, so you can use FindControl("pointsTextBox") to get a reference to the textbox control.
You could do something like:
TextBox t = this.FindControl("pointsTextBox") as TextBox;
if(t != null)
{
ddListPlayerPOintSystems.Add(new { COntrolID = t, Name = "InsertPts", PropertyName="Text", Type="Decimal"});
}
I haven't tested it, so I am not claiming the code is perfect, but the method you need to follow is illustrated here.

Set SelectParameter for SqlDataSource SelectCommand with unknown number of variables

<asp:SqlDataSource ID="workHourListSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:PlaningSystemConnectionString %>"
SelectCommand="SELECT *
FROM Date AS t1 FULL OUTER JOIN (SELECT * FROM WorkHoursEntry WHERE (WorkerID LIKE #WorkerID)) AS t2 ON t1.PKDate = t2.WorkDay
LEFT OUTER JOIN Worker AS t3 ON t2.WorkerID = t3.WorkerID
LEFT OUTER JOIN (SELECT * FROM Project WHERE ProjectID IN(#Project)) AS t4 ON t2.ProjectID = t4.ProjectID"
FilterExpression="YearMonth IN({0})">
<FilterParameters>
<asp:ControlParameter ControlID="yearMonthFilterLabel" Name="YearMonth"
PropertyName="Text" Type="String" />
</FilterParameters>
<SelectParameters>
<asp:ControlParameter ControlID="WorkerIDLabel" Name="WorkerID"
PropertyName="Text" Type="String" />
<asp:ControlParameter ControlID="projectFilterLabel" Name="Project"
PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
Greetings, I have problem with #Project ControlParameter, problem is I don't know how to pass several values and FilterParameters method won't work as I want. Is there any way to pass several variables ? (without using c#).
projectFilterLabel.Text = 'aaa, bbb, ccc';
Basically what I want to do is to select all values from Date table and join them with some values from Project table and output to DataList.
Any help appreciated, I just started learning asp.net
Thanks.
In your asp form create a javascript function to handle the Opening of the window fro your report. In this function set a parameters (var pPassParm) value to a textbox.text (hidden on your parent form) and pass this value with opening of the window ( /reportfolder/report.asx?pPassparm )
On your execution code, create a piece of code that concatenates all your parameters into one string ( textbox.text = 'StartDate = 2012/01/01, EndDate = 2012/02/01, Surname = Jones' etc) before opening the new form for the report.
Refer to post Open a new window and pass parameters to it vb.net asp
I solved this problem by creating new querys with parameters inside (replacing older ones) for SqlDataSource on events.

Resources