473,806 Members | 2,565 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String format strange behaviour

Hi,

I have the following line in my code:

strConnect = String.Format(" Data Source={0},{1}; Network
Library=DBMSSOC N;Initial Catalog={2};Int egrated 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=DBMSSOC N;Initial
Catalog=test;In tegrated Security=False; Trusted
Connection=Yes; UID=test1;PSW=t est2"

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 1948
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.co mschreef 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=DBMSSOC N;Initial Catalog={2};Int egrated 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=DBMSSOC N;Initial
Catalog=test;In tegrated Security=False; Trusted
Connection=Yes; UID=test1;PSW=t est2"

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.co mschreef 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=DBMSSOC N;Initial Catalog={2};Int egrated 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=DBMSSOC N;Initial
Catalog=test;In tegrated Security=False; Trusted
Connection=Yes; UID=test1;PSW=t est2"

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.co mwrote:
I have the following line in my code:

strConnect = String.Format(" Data Source={0},{1}; Network
Library=DBMSSOC N;Initial Catalog={2};Int egrated 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=DBMSSOC N;Initial
Catalog=test;In tegrated Security=False; Trusted
Connection=Yes; UID=test1;PSW=t est2"

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.co m>
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.co mwrote:
found where the problem originates... it's the encryption class before it...
Let me guess - Encoding.GetStr ing(data) instead of
Encoding.GetStr ing(data, 0, decryptedLength )?

--
Jon Skeet - <sk***@pobox.co m>
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.co mschreef in bericht
news:MP******** *************@m snews.microsoft .com...
EricW <ie****@home.co mwrote:
>I have the following line in my code:

strConnect = String.Format(" Data Source={0},{1}; Network
Library=DBMSSO CN;Initial Catalog={2};Int egrated 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=DBMSSOC N;Initial
Catalog=test;I ntegrated 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.co m>
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
1844
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 not added. Please comment. Neil PEP: 349 Title: Generalised String Coercion
7
6499
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 where I can look up explaination on the String.Format, the format string part like "("{0:.#0}". What's the trailing 0 and what is that "0:" means? I assume the "#" is the place holder character for th value obtain after the comma in the code. ...
3
485
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 & c & d & e +/- 420ms
20
3723
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 MyVariant = 2 Then MsgBox "MyVariant also equals the value 2" 160 If MyVariant = "" Then HowManyCopies = 1 170 If Not IsNumeric(MyVariant) Then HowManyCopies = 1 MsgBox "OK. HowManyCopies has a value of " & CStr(HowManyCopies) 180 For i =...
6
5242
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 that very same format string from an xml file: ....load xmlNode WorkNode...
1
10699
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 Message="Input string was not in a correct format." Source="mscorlib"
13
10398
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 being convert to an Int16 (using Convert.ToInt16). How can that be? There are other text boxes that are used in the identical fashion and they don't generate the exception. All there are many other machines running my application that don't...
4
2289
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 call a static method: public static string ValidateMessage(params...)
8
5323
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. (Sorry, long email) The first two examples are behaving normal, the thirth is strange....... I wrote the following flabbergasting code: #-------------------------------------------------------------
0
9597
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10620
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10110
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5682
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3851
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.