473,320 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

String format strange behaviour

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
Sep 17 '08 #1
5 1905
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.


"EricW" <ie****@home.comschreef in bericht
news:48***********************@news.kpnplanet.nl.. .
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


Sep 17 '08 #2
found where the problem originates... it's the encryption class before it...

"EricW" <ie****@home.comschreef in bericht
news:48***********************@news.kpnplanet.nl.. .
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


Sep 17 '08 #3
EricW <ie****@home.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 17 '08 #4
EricW <ie****@home.comwrote:
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)?

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 17 '08 #5
You've guessed it right, the null values terminated my stringbuild. :-)

Thanks, though!
Eric

"Jon Skeet [C# MVP]" <sk***@pobox.comschreef in bericht
news:MP*********************@msnews.microsoft.com. ..
EricW <ie****@home.comwrote:
>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.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com

Sep 17 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Neil Schemenauer | last post by:
The title is perhaps a little too grandiose but it's the best I could think of. The change is really not large. Personally, I would be happy enough if only %s was changed and the built-in was...
7
by: Alpha | last post by:
Hi, I'm maintaining C# code and am fairly new with C# programming. I'm looking for codes that's droping the 2nd digit of a nuber printed out and I suspect it's the code below. Can someone tell me...
3
by: Dominique Vandensteen | last post by:
after the very small & vs string.format discussion I did some speed tests... loop of 1.000.000 concatenations of 5 public string variables in a class gave following results: result = a & b...
20
by: MLH | last post by:
120 MyString = "How many copies of each letter do you need?" 150 MyVariant = InputBox(MyString, "How Many?", "3") If MyVariant = "2" Then MsgBox "MyVariant equals the string '2'" If...
6
by: Scewbedew | last post by:
Suppose I have the following code: string myFormat = "Line1/nLine 2"; string formattedString = string.Format(myFormat); ....that would produce a 2-line output as expected. But if I load...
1
by: Remi THOMAS | last post by:
Hi, When you execute this line of code string script = string.Format("foreach (GroupDoc gdoc in {0}.SetGroup(\"{1}\") {\r\n", "p1", "p2"); You get System.FormatException was unhandled...
13
by: Jen | last post by:
One user of my application is experiencing an exception "input string not in correct format". But it makes no sense where it is occurring. It is occurring when a string from a textbox ("172") is...
4
by: BA | last post by:
Hello, I have a very strange code behavior that I cannot make heads or tails of: I have c# code being executed in BizTalk assemblies which is acting very strangely. In my BizTalk process I...
8
by: Dox33 | last post by:
I ran into a very strange behaviour of raw_input(). I hope somebody can tell me how to fix this. (Or is this a problem in the python source?) I will explain the problem by using 3 examples....
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.