C# Inserting % into a string

  • Thread starter Thread starter johnp
  • Start date Start date
J

johnp

I have the following line:

string SearchValue = string.Format("Select ACCOUNTID,
ACCOUNT from ACCOUNT where ACCOUNT LIKE \'{0:#%}\' order
by ACCOUNT, SearchString.Trim()");

I have tried just about everything to insert a % into my
string. In the past this has not been an issue since I
mostly use stored procedures. I am running some test
queries on a database that I do not want to add new
stored procedures to.

SearchString comes from a text box on my test form.

The above code returns the following error:
Index (zero based) must be greater than or equal to zero
and less than the size of the argument list.

Suggestions greatly appreciated.
 
Hi johnp,

I'm guessing "SearchString.Trim()" was intended to be the second
parameter of the method call, but you included it within the string literal
passed as the first parameter to String.Format.

Regards,
Dan
 
SearchString was supposed to be the first paramter.

Adjusting the code as follows:

string SearchValue = string.Format("Select ACCOUNTID,
ACCOUNT from ACCOUNT where ACCOUNT LIKE \'{0}{1:#%}\'
order by ACCOUNT, SearchString.Trim()");

Still generates the same error messsage.

Help still needed. I am not sure what the C# archetect
team was thinking when they restricted certain characters
from normal strings... Delphi is more flexible.
 
Hello:

Try:

string SearchValue = string.Format("Select ACCOUNTID,
ACCOUNT from ACCOUNT where ACCOUNT LIKE \'{0}{1:#%}\'
order by ACCOUNT", SearchString.Trim());
 
Carlos,

I am sorry I guess I had a typo when I pasted that
previous message. There actually was a double quote after
that last ACCOUNT before the comma.

The issue is still here.


Regards,

JohnP
 
Carlos,

I am sorry I guess I had a typo when I pasted that
previous message. There actually was a double quote after
that last ACCOUNT before the comma.

The issue is still here.


Regards,

JohnP
 
Why not do the following?

string SearchValue = string.Format("Select ACCOUNTID, ACCOUNT from ACCOUNT
where ACCOUNT LIKE \'{0}%\' order by ACCOUNT", SearchString.Trim());

The result I obtain is:

"Select ACCOUNTID, ACCOUNT from ACCOUNT where ACCOUNT LIKE '15%' order by
ACCOUNT"

Tyler
 
Tyler,

Boy that sure worked. I guess I was mislead by a site
that had an example on how to use % in a string and they
suggested the {0:#%} and since nothing else was working I
went along the wrong path.

Thank you for the assistance everyone!

Regards,

JohnP
 
Back
Top