Asp:EntityDataSource: Make a insentitive diacritics search in where clause - asp.net

I can't figure out how to do this.
I have a Gridview bind on a asp:EntityDataSource. This asp:EntityDataSource returns me a list of employe and the EntityDataSource can be filtered by a textbox on the name of the employee. I'm looking for a way to search by name being diacritics insentitive.
(Ex: Searching for "theroux" could return me "théroux" and/or "theroux". Or searching for "théroux" could return me "theroux" and/or "théroux")
I try searching in the canonical function but i didn't find anythings. Is there a way to do this without any code behind?
Here is my EntityDataSource
<asp:EntityDataSource ID="edsEmployes" runat="server"
ConnectionString="name=MyEntities" DefaultContainerName="MyEntities"
EntitySetName="Employes"
where="it.Name like '%'+#keyword+'%' ">
<WhereParameters>
<asp:ControlParameter ControlID="txtKeyword" DbType="String"
DefaultValue="%" Name="keyword" PropertyName="text" />
</WhereParameters>
</asp:EntityDataSource>

Assuming you're using SQL Server for you database I would try changing the collation on that field in the database to SQL_Latin1_General_CP1_CI_AI. The AI at the end means accent insensitive which should filter as you are wanting it to.

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.

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.

Relationship change from one-many into Many to many need to update listview

I have a distribution table with a pk of DistributionID and a recipients table with RecipientID as pk. This table used to be 1 to many but now needs to change to a many to many with an intermedate table.
I have an EntityDataSource that supplies a listview that allows for simple manipulation of the distribution list.
<asp:EntityDataSource ID="edsRecipients" runat="server" ConnectionString="name=DistributionEntities" DefaultContainerName="DistributionEntities"
EnableDelete="True" EnableFlattening="False" EnableInsert="True" EnableUpdate="True" EntitySetName="Recipients"
Where="it.[DistributionID]=#DistributionID">
<WhereParameters>
<asp:ControlParameter ControlID="ddlSelectDistributionList" ConvertEmptyStringToNull="true" DbType="Int32" Name="DistributionID" />
</WhereParameters>
</asp:EntityDataSource>
Is there any way i can change the where clause to work with the new table New table is named DistributionRecipients if that is needed. If not is there some way i can bind the Distribuion.Recipients of the EF Class to the List view in the code behind that will work with the automatic edit and delete functionality or will i need to add code to handle them?
You can modify the where attribute of your EntityDataSource to include a sub-query on the Distributions navigation property of the Recipients entity:
EXISTS(SELECT Distributions.DistributionID FROM it.Distributions WHERE Distributions.DistributionID=#DistributionID)

EntityDataSource replace * with % wildcard on queries

I have an application that uses EntityDataSource in many places.
In the EDS, I manually build the Where clause based on user input from TextBox'es.
I would like the user to be able to enter "*" (asterisks) instead of "%" when querying data.
Is there an easy as using Entity SQL or the EDS itself to do a search/replace? I know I could actually change the TextBox after the data is entered, but when the user sees his text was changed from an * to a % I don't think he will understand.
I have tried using the T-SQL Replace command and doing something like this:
<asp:EntityDataSource ID="EDSParts" runat="server"
ConnectionString="name=TTEntities" DefaultContainerName="TTEntities"
EnableFlattening="False" EntitySetName="Parts"
OrderBy="it.ID DESC"
Where ="(CASE
WHEN (#PartNumber IS NOT NULL) THEN
it.[Number] LIKE REPLACE(#PartNumber, "*", "%")
ELSE
it.[ID] IS NOT NULL
END)">
<WhereParameters>
<asp:ControlParameter Name="PartNumber" Type="String"
ControlID="txtPartNumberQuery" PropertyName="Text" />
</WhereParameters>
</asp:EntityDataSource>
But I get a "Server tag is not well formed" message. I can't find an equivalent "replace" function in the Entity SQL reference....
Any ideas?
You can handle page postback and modify content of txtPartNumberQuery. EntityDataSource can work only with % (because it builds ESQL query) so you have to change * to % in your codebehind before you execute databinding.
Sluama - Your suggestion fixed it! Such an obvious answer. The " was terminating the Where clause string. I could have sworn I tried that, but I guess not. Becuase, I just happened to come back to this question and saw your answer and it works!
<asp:EntityDataSource ID="EDSParts" runat="server"
ConnectionString="name=TTEntities" DefaultContainerName="TTEntities"
EnableFlattening="False" EntitySetName="Parts"
OrderBy="it.ID DESC"
Where ="(CASE
WHEN (#PartNumber IS NOT NULL) THEN
it.[Number] LIKE REPLACE(#PartNumber, '*', '%')
ELSE
it.[ID] IS NOT NULL
END)">
<WhereParameters>
<asp:ControlParameter Name="PartNumber" Type="String"
ControlID="txtPartNumberQuery" PropertyName="Text" />
</WhereParameters>
</asp:EntityDataSource>

Filter expression not working properly

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?

Resources