Solved

Where Query Syntax Help in  C# Code Behind

Posted on 2011-09-30
4
232 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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…

737 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