Linq Querying for user search - wpf

I am trying to query from search box using list of data and the linq I used is:
data = (From k As BSPLib.ContactLib.Contact In data_org.Values Where k.stringdata Like "%" & Searchtxtbox.Text & "%" Select k.prime).ToList
But this is not working, I am getting no data at all. Data_org is a dictionary so I used the values; k.stringdata contains all the data that need to be searched. Searchtxtbox.text contains the user defined search item.
I Tried with sqlmethods through linq, but sqlmethods does not exist for me, I tried with with Imported namespace yet the code is not showing sql methods, could you please provide a workable query or just tell me where I have gone wrong. Thank you.

My Visual Basic is a bit rusty so forgive me if I have the syntax wrong:
One thing you could use is Contains which will be similar to "%Searchtxtbox.Text%"
and exactly the same if it is used with a DatabaseContext.
I know it is not the same as Like but if that doesn't work than likely something else is wrong and than I would like more code.
data = (From k As BSPLib.ContactLib.Contact In data_org.Values Where k.stringdata.Contains(Searchtxtbox.Text) Select k.prime).ToList
You can additionally use StartsWith and EndsWith for "Searchtxtbox.Text%" and "%Searchtxtbox.Text"
Also would I like to suggest to puth "%" & Searchtxtbox.Text & "%" between Parentheses for clarification, but that's all up to you.

The % wildcard character won't work here, remember this is .NET code, not SQL. You need something like this instead:
data = (
From k As BSPLib.ContactLib.Contact In data_org.Values
Where k.stringdata.Contains(Searchtxtbox.Text) Select k.prime).ToList

Related

SSRS adds new line when concatenating into a textbox

I'm stuck here. Everytime I concatenate SSRS textbox, the concatenation adds a new line. I use a Microsoft SQL 2013 database. Here is my expression that I use to concatenate.
=First(Fields!FirstName.Value, "DataSet1") & " " & First(Fields!LastName.Value, "DataSet1)
What happens is that there is a new line placed after the first name making the final output look like this.
FirstName
LastName
If anyone has any suggestions that would be great. I tried googling the problem and I couldn't find an answer. Please someone help.
Try using a variable under Report/Report Properties or even concat it on the query end...it may be easier
Perhaps you have leading/trailing spaces. Try using LTRIM or RTRIM.
=First(LTRIM(RTRIM(Fields!FirstName.Value)), "DataSet1")
& " " & First(LTRIM(RTRIM(Fields!LastName.Value)), "DataSet1)
I have found what was wrong. I had to select my expression (Expr). Then in the properties tab, I changed the Markup Type to HTML and that fixed my problem.
Thank you everyone for your time and effort.

peewee get_or_create and then save: error binding

Is there an easy way to update a field on a get of a get_or_create?
I have a class ItemCategory and I want to either create a new entry or get the already created entry and update a field (update_date).
What I do is:
item,created= ItemCategory.get_or_create(cat=cat_id,item=item_id)
if created == True:
print "created"
else:
item.update_date = datetime.now
item.somethingelse = 'aaa'
item.save()
This works for a while in my loop. But after 50-100 get/create it crashes:
peewee.InterfaceError: Error binding parameter 4 - probably unsupported type.
Maybe I should use upsert(), I tried but wasn't able to get anything working. Also it's not probably the best solution, since it makes a replace of the whole row instead of just a field.
I like peewee, it's very easy and fast to use, but I can't find many full examples and that's a pity
Newbie mistake
item.update_date = datetime.now()
I am not 100% sure this is the only answer though. I modified my code so many times that it might be also something else.
Regarding my question about create_or_update , I've done this:
try:
Item.create(...)
except IntegrityError:
Item.update(...)
peewee is really great, I wonder why no one ever asked for a create_or_update.

Reorder comma separated sentences into new paragraphs

Above is a database that i am uploading to a hosting server to pull the data off of it to a website using .asp code in a HTML file. The sensors field gets the data using lookup from another table and can contain multiple values but these values are separated using a comma ",". I was wondering if there was a way either within the database or on the .asp page to get rid of the comma and sort them in a new paragraph or replace the "," with "< br/ >" between each of the sentences
NOTE CHANGE TO ORIGINAL QUESTION
The original question was in reference to ASP.NET. After providing a thorough answer it transpired that the author was actually using VBScript and Classic ASP. The final update to this answer reflects his change.
I have left my original answer just in case it helps someone else with the same issue as originally asked..
===================================
Replace won't be enough if you want to change any of the order; I.E alphabetically.
One way would be to put the Database string into a list, splitting it at the comma. you can then output this list in any order you want.
For example.
to Split into a list
List<string> myList = yourSensorsString.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
You can then order this however you see fit. Something like:
myList.Sort();
Then simply go through each of the items, like - where myLbl is perhaps a label control on your page
foreach ( str in myList) {
myLbl.Text += str + "<br />";
}
Or as separate paragraphs, maybe into a Literal
foreach ( str in myList) {
myLit.Text += "<p>" + str + "</p>";
}
Update
This code example above would be placed in your code-behind. You would get your sensors string from the database, and then split it into a list, then output to the HTML. No need to get your database to perform this sort of logic.
I have also assumed C# as you never mentioned what flavour of .net you're using.
Update 2
After looking at your fiddle, i edited it slightly to show where you can use this sort of code. Also converted my code to VB. Hope this helps. Can't put any more time into this... Fiddle Is Here
Update 3
The original question was for ASP.NET - as it turns out, it's actually Classic ASP the OP is asking about.
So split your string like this:
myList=Split(rsLogbook("FieldName"), ",")
for each str in myList
document.write(str & "<br />")
next
Other than that, I can't help any further - lot of time spent on this :)

SSRS - Adding "LIKE" filter criteria to report builder

I want to add a LIKE filters with wildcards in SSRS report builder. I tried this using contains clause present in filter data section of report builder. I tried both '*' and '%', but of them failed.
I tried
MyFieldName contains 2451 - this succeds
MyFieldName contains 24* - this fails
MyFieldName contains 24% - this fails
From below link I feel that this is an old problem with no solution till yet.
http://connect.microsoft.com/SQLServer/feedback/details/202792/ssrs-adding-like-filter-criteria-to-report-builder
What do you guys suggest?
Thanks
Ravi Gupta
You could use the InStr function
=IIF(InStr(Fields!MyFieldName.Value, "2451"),TRUE,FALSE)
In SSRS we can't use Like. Instead of which you can use Contains.
IIF((MyFieldName).ToString().Contains("RequiredString"),"True","False)
In the report builder in Visual studio there is a Like and it works. Use a * as wildcard in your search string
Answering my own question, Below function worked for me:
IF(FIND(MyFieldName, "24") = 1, TRUE, FALSE)
This is just a partial answer as this will work for cases like (blabla*), but its not for cases like (bla*bla, blabla*). So anyone having a better idea is most welcome.
Got idea to do this from Darren's comment above.
Thanks
Ravi Gupta
The Report Builder does not support the LIKE operator. You must use CONTAINS;
This is pretty late to the party, but I worked out a solution for parameter filters for my dataset.
Adding a filter to my dataset with
Expression:
=IIF(InStr(Fields!AccountName.Value, Parameters!paramAccountName.Value) OR Parameters!paramAccountName.Value = "*", TRUE, FALSE)
Type
Boolean
Operator
=
Value
true
The parameter are defaulted to "*" so the report looks like it grabs everything by default.
I just came across this old thread and I'm curious why you can't use the like keyword, since I've always used it.
Did you try something like this?
Where (AccountName like '%' + #paramAccountName + '%' or #paramAccountName ='')

C#, Linq data from EF troubles

I don't have much experience with programming. I am working (to learn) on a project. I am using C# 4.0 and WPF 4 with EF (SQLite). I am having problemw with LINQ.
Here is the code in question (I hope this is enough, let me know if more is needed)
private void cboSelectCompany_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
using (ShippingEntities context = new ShippingEntities())
{
var companies = from company in context.Deliveries
where company.State == cboSelectCompany.SelectedItem.ToString()
select company;
txtDeliveryName.Text = companies.Name;
txtDeliveryState.Text = companies.State;
}
The last two lines don't work. Am I misunderstanding what LINQ is returning? I just get this error
Error 5 'System.Linq.IQueryable<SqliteDemo.Delivery>' does not contain a definition for 'State' and no extension method 'State' accepting a first argument of type 'System.Linq.IQueryable<SqliteDemo.Delivery>' could be found (are you missing a using directive or an assembly reference?) c:\users\dan\documents\visual studio 2010\Projects\SqliteDemo\SqliteDemo\DeliveryCompanies.xaml.cs 49 51 SqliteDemo
If anyone could give me some pointers or to a good reference I would appreciate it
You're close!
Linq is returning a Queryable set of Deliveries, which hasn't executed yet. My guess based on reading your code is that you're expecting at most one result, and then you want to put those values into a UI of some sort. In that case what you'll want to do is:
Take your IQueryable and execute it
Make sure you grab the first (or, alternatively enforce that there is only one) row
Set the appropriate values in the UI.
So, leave the line with the query there, and then change the rest to something like this:
var company = companies.FirstOrDefault();
txtDeliveryName.Text = company.Name;
txtDeliveryState.Text = company.State;
Insert null-checking as appropriate, and go check out the differences between First, FirstOrDefault, Single, and SingleOrDefault to see which one seems most correct for your particular situation.
Well, looks like companies.State doesn't compile, because companies is a list of SqliteDemo.Delivery (and not just a SqliteDemo.Delivery).
What do you want to achieve? Imagine that your LINQ query returns several results, is it possible?
Just use companies.FirstOrDefault() or companies.Single() to access first item becouse by default LINQ returns collection of items not just single item.

Resources