Help with special chars in csharp strings?

A

Alex

Hi all -

Is there a standard way to handle special chars in strings in dotnet? I'm
using csharp and having two seperate problems...

1. Passing in args...

I need to pass in an arg that points to a sqlserver2005 instance... it
comes in looking like this in the debugger:

I pass it in using this arg: MyServer\sql2005

In the debugger it displays as MyServer\\sql2005 - however it errors when I
try to make a dbconnection using it.

If I do this after receiving the arg, but before establishing the
connection then it still looks like MyServer\\sql2005 in the debugger, but
the connection is established.

sServer = sServer.Replace(@"\", "XXXXXXX");
sServer = sServer.Replace("XXXXXXX", @"\");


2. Special chars from the DB.

I have a DB field that I load into a string and then display in a textbox.
This field is allowed to have quotes. When I load it from the DB, it
displays the text (including the quotes) correctly as 'bang'.

However, when I write it back the ExecuteNonQuery fails since it tried to
build up the update string using the quotes without escaping them.

Is there a special string type I should be using to handle this, or do I
need to preprocess the string each time before building up my update
statment?

Seems like there should be a general way to handle special chars instead of
dealing with them all individually...

Thanks!
 
J

John B

Alex said:
Hi all -

Is there a standard way to handle special chars in strings in dotnet? I'm
using csharp and having two seperate problems...

1. Passing in args...

I need to pass in an arg that points to a sqlserver2005 instance... it
comes in looking like this in the debugger:

I pass it in using this arg: MyServer\sql2005

In the debugger it displays as MyServer\\sql2005 - however it errors when I
try to make a dbconnection using it.

If I do this after receiving the arg, but before establishing the
connection then it still looks like MyServer\\sql2005 in the debugger, but
the connection is established.

sServer = sServer.Replace(@"\", "XXXXXXX");
sServer = sServer.Replace("XXXXXXX", @"\");
The double \ is just the ide escaping the \.
If you look at the string with the text visualizer then you should see
the correct string with one \.
There must be something else different when you try the second time.
2. Special chars from the DB.

I have a DB field that I load into a string and then display in a textbox.
This field is allowed to have quotes. When I load it from the DB, it
displays the text (including the quotes) correctly as 'bang'.

However, when I write it back the ExecuteNonQuery fails since it tried to
build up the update string using the quotes without escaping them.
<....>

Use an sqlcommand object and sqlparameter objects.
 
A

Alex

The double \ is just the ide escaping the \.
If you look at the string with the text visualizer then you should see
the correct string with one \.
There must be something else different when you try the second time.

Seems like there must be something going on since the net effect of the
above two statements should be no effect. However, if I comment them out
it can't connect to the server - uncommented it works fine.

Strange...

Thx for the reply!
 
B

Ben Voigt

Alex said:
Seems like there must be something going on since the net effect of the
above two statements should be no effect. However, if I comment them out
it can't connect to the server - uncommented it works fine.

Why would the net effect be nothing? Try @"XXXX\XXXX"...
After the first substitution it is @"XXXXXXXXXXXXXXX", after second
substitution it's @"\\X"
Not the same at all, and note that the string "XXXXXXX" didn't appear in the
original at all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top