2

I would like to know how to give a space in the string for the vaue field in web.config file. for example <add key="number" value="0001 " />

I would like to have a space entered after '1' in the above value field,I tried giving a space directly on to the value field,but this does not occur.

Kindly share your opinion/replies on the same.

Hi all, Sorry for not being clear.. This is what i want, am adding the following in the web.config file '' I use this as a prefix in the text box where once a user clicks on a particular key, the above prefix is entered in the text box allowing the user to suffix the net few digits only.

So in my code

Tbx.text=app.DeploymentConfigurations["number"];

But i want a space after the 0001 to also be prefixed. How do i give that in the key value.


Note: i do not want the space to be hard coded.. I have already done that. Space is a requirement now, but it might change later. So it would be better if i have the spacce included in my app settings so that user will have to change it only on the config file and not on the code. Thank you all

chriga
  • 798
  • 2
  • 12
  • 29
user1443320
  • 31
  • 1
  • 1
  • 3
  • Are you referring to appSettings? – Kyle Trauberman Jun 07 '12 at 22:10
  • 1
    would u write the sample config string here to know what your actual requirements are? – AvkashChauhan Jun 07 '12 at 22:11
  • @jesus.tesh config files are not urls, so url encoding may not be applicable, unless the value the OP mentions is a url. – Kyle Trauberman Jun 07 '12 at 22:13
  • Just a guess but would   work? – Gene S Jun 07 '12 at 22:18
  • 1
    @Kyle - true, but reading the string in .NET should allow it to be correctly interpreted. There is also the Unicode if %20 doesn't work. – Tim Hobbs Jun 07 '12 at 22:19
  • 1
    Perhaps you should take a different approach and handle the spaces within the code that binds to your textbox. If you want to append 2 key values, just add the space and/or prefix in your code and NOT in the config. – Tim Hobbs Jun 07 '12 at 22:22
  • 1
    One last possible idea - maybe use an underscore? If needed, in code you could replace underscores with spaces? It is a bit hacky, but it may get the job done. – Tim Hobbs Jun 07 '12 at 23:01

4 Answers4

1

Without seeing more code, it's hard to tell what's going on; as per D Stanley's answer, this worked for me.

However, since you are dealing with user input, it's probably best to enforce the prefix server side anyway; a few options would include:

1) Putting the prefix in a separate textbox or label that the user can't edit in front of the user editable content. This should eliminate the possibility that your prefix is being typed over by the user.

2) You could format your text server side; there's no need to hard code it either, simple:

<add key="Prefix" value="0001" />
<add key="Separator" value=" " />

You can then do something like:

string overall = String.Format("{0}{1}{2}", ConfigurationManager.AppSettings["Prefix"], ConfigurationManager.AppSettings["Separator"], userInputtedText);

The user can then of course remove the space or change it to a different character or characters independently of anything else.

dash
  • 89,546
  • 4
  • 51
  • 71
0

Why not just add the space in code?

Tbx.text=app.DeploymentConfigurations["number"] + " ";

Since your requirements require you to have the space in the config, try this:

<add key="number" value="0001&#x20;" />

&#x20; is the space character in unicode.

Barring that, use a magic string:

<add key="number" value="0001{_}" />

Then replace in code:

Tbx.text=app.DeploymentConfigurations["number"].Replace("{_}", " ");

This will allow you to control the placement of the space in the config, while still achieving the results you are looking for.

Kyle Trauberman
  • 25,414
  • 13
  • 85
  • 121
  • I had done that, but the user wants to change it only in the config file later and not touch the code – user1443320 Jun 07 '12 at 22:24
  • Moreover the space is only required as of now, it might not be needed later, so i would like to have it only in the app settings rather than hard code it. – user1443320 Jun 07 '12 at 22:27
  • Hi, again adding the unicode as you said would be in my C# code and not on the config file. It would be difficult for the user to find this particular line to remove the space.Thank you for quick reply. – user1443320 Jun 07 '12 at 22:45
  • Hi Kyle,I did exactly what you had asked me to, but it did not work.Thanks for your efforts to help me. – user1443320 Jun 07 '12 at 22:58
  • Then you could do something like include a "magic string" (like `{_}` for example) in your config value, check for it in code and replace with a space. This will still allow the adding of the space to be controlled from the config file, while achieving the results you want. – Kyle Trauberman Jun 07 '12 at 23:01
  • I mean i did and read the same from # code as Tbx.text=app.DeploymentConfigurations["number"]; Am i missing anything? – user1443320 Jun 07 '12 at 23:03
  • Nope, that's what I meant. I noticed in another comment that you're using silverlight, so that definitely wouldn't have worked. Try the magic string idea. – Kyle Trauberman Jun 07 '12 at 23:07
  • 1
    Hi Kyle,Thanks a lot, I did a little alteration to what you said, but implemented an idea similar to it. Thanks a ton for your help.I appreciate it. Thank you once again. – user1443320 Jun 08 '12 at 00:05
0

This behavior is actually part of Raw XML parsing as discussed here:

I just verified the the settings as below using @Kyle suggestion as below and it did work so Kyle suggestion can be used as Answer:

In your code, I think the best way to do is as below:

string s = app.DeploymentConfigurations["number"];
Tbx.text= s.PadRight(s.Length + 1);

This will add 1 space to your value.

Community
  • 1
  • 1
AvkashChauhan
  • 20,495
  • 3
  • 34
  • 65
  • I had done that, but the user wants to change it only in the config file later and not touch the code Moreover the space is only required as of now, it might not be needed later, so i would like to have it only in the app settings rather than hard code it – user1443320 Jun 07 '12 at 22:31
0

Are you using the standard AppSettings element or some custom settings provider? This works for me:

app.config:

<appSettings>
  <add key="Test" value="123456 "/>
</appSettings>

code:

string setting = ConfigurationManager.AppSettings["Test"];
Debug.Assert(setting.Length==7);  // returns true

Is something trimming the string after it's pulled from web.config? How are you using the setting as a prefix?

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • Hi Stanley, Mine is a silverlight project. And i am not trimming after it pulls from the web.config file. For me it shows only 4 as the length for "0001 ". – user1443320 Jun 07 '12 at 22:41