Solved

Where Query Syntax Help in  C# Code Behind

Posted on 2011-09-30
4
231 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

756 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