Solved

Where Query Syntax Help in  C# Code Behind

Posted on 2011-09-30
4
229 Views
Last Modified: 2013-12-16
Hope this is an easy one.  I have a combobox in silverlight that is querying from a database service with a where clause.  The data has commas so it works when using the single quotes, but I can't figure out the query code in C# to get the single quotes to work.  

This works outside of Visual Studio
(MBR_112th = 'LastName, FirstName MiddleInitial.')


queryName.OutFields.AddRange(new string[] { "MBR_112TH", "DIST", "WEBSITE", "PERSONAL_WEBSITE" });
queryName.Where = "MBR_112TH = " + QueryComboBoxName.SelectedIndex.ToString();

Open in new window

0
Comment
Question by:Gregory3
  • 2
4 Comments
 
LVL 10

Accepted Solution

by:
dwe761 earned 250 total points
ID: 36892835
did you try this?
queryName.OutFields.AddRange(new string[] { "MBR_112TH", "DIST", "WEBSITE", "PERSONAL_WEBSITE" });
queryName.Where = "MBR_112TH = '" + QueryComboBoxName.SelectedIndex.ToString() + "'";

Open in new window

0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
ID: 36894152
You might find it a tad more readable to use string.Format, though dwe761's approach should work as well:

queryName.OutFields.AddRange(new string[] { "MBR_112TH", "DIST", "WEBSITE", "PERSONAL_WEBSITE" });
queryName.Where = string.Format"MBR_112TH = '{0}'",  QueryComboBoxName.SelectedIndex.ToString());

Open in new window


where "{0}" gets substituted for the first parameter after the format string (i.e. the first parameter to string.Format).
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36894156
P.S.

I forgot an opening parentheses on the function call. Corrected below:
queryName.OutFields.AddRange(new string[] { "MBR_112TH", "DIST", "WEBSITE", "PERSONAL_WEBSITE" });
queryName.Where = string.Format("MBR_112TH = '{0}'",  QueryComboBoxName.SelectedIndex.ToString());

Open in new window

0
 

Author Closing Comment

by:Gregory3
ID: 36905533
Thanks guys.  Both worked in different situations.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question