String format strange behaviour

E

EricW

Hi,

I have the following line in my code:

strConnect = String.Format("Data Source={0},{1};Network
Library=DBMSSOCN;Initial Catalog={2};Integrated Security=False;Trusted
Connection=Yes;UID={3};PSW={4};", myServer, myDBPort, Database.Name,
myDBUsername, myDBPassword)


with: myServer = "1.2.3.4"
myDBPort = "1344"
Database.Name = "test"
myDBUsername = "test1"
myDBPassword = "test2"

the strConnect should look like:

"Data Source=1.2.3.4,1344;Network Library=DBMSSOCN;Initial
Catalog=test;Integrated Security=False;Trusted
Connection=Yes;UID=test1;PSW=test2"

But.... it looks like this: "Data Source=1.2.3.4

No ending " and no rest of the string.
If I take out the variables and just hardcode the info, there is no problem.

Why is the string not ending with " after the IP address?
The same happens if I hard-code the IP. Then there is no ending " after the
portnumber.

Can someone help me with this? I'm stuck now...

rg,
Eric
 
E

EricW

Just noticed that the variable myServer is not "1.2.3.4" but "1.2.3.4

Which is strange since it is set by a string property from a class.
 
J

Jon Skeet [C# MVP]

EricW said:
I have the following line in my code:

strConnect = String.Format("Data Source={0},{1};Network
Library=DBMSSOCN;Initial Catalog={2};Integrated Security=False;Trusted
Connection=Yes;UID={3};PSW={4};", myServer, myDBPort, Database.Name,
myDBUsername, myDBPassword)


with: myServer = "1.2.3.4"
myDBPort = "1344"
Database.Name = "test"
myDBUsername = "test1"
myDBPassword = "test2"

the strConnect should look like:

"Data Source=1.2.3.4,1344;Network Library=DBMSSOCN;Initial
Catalog=test;Integrated Security=False;Trusted
Connection=Yes;UID=test1;PSW=test2"

But.... it looks like this: "Data Source=1.2.3.4

No ending " and no rest of the string.
If I take out the variables and just hardcode the info, there is no problem.

Why is the string not ending with " after the IP address?
The same happens if I hard-code the IP. Then there is no ending " after the
portnumber.

Can someone help me with this? I'm stuck now...

How are you displaying the result? My *guess* is that you're using a
windows forms control, which will see a null character (\u0000) as a
string terminator - so I suspect you've got a null character in
myServer. How are you getting that data?

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
J

Jon Skeet [C# MVP]

EricW said:
found where the problem originates... it's the encryption class before it...

Let me guess - Encoding.GetString(data) instead of
Encoding.GetString(data, 0, decryptedLength)?
 
E

EricW

You've guessed it right, the null values terminated my stringbuild. :)

Thanks, though!
Eric
 

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