String format strange behaviour

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • EricW

    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


  • EricW

    #2
    Re: String format strange behaviour

    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" <iemand@home.co mschreef in bericht
    news:48d0bb0a$0 $27221$9a622dc7 @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
    >
    >

    Comment

    • EricW

      #3
      Re: String format strange behaviour

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



      "EricW" <iemand@home.co mschreef in bericht
      news:48d0bb0a$0 $27221$9a622dc7 @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
      >
      >

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: String format strange behaviour

        EricW <iemand@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 - <skeet@pobox.co m>
        Web site: http://www.pobox.com/~skeet
        Blog: http://www.msmvps.com/jon.skeet
        C# in Depth: http://csharpindepth.com

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: String format strange behaviour

          EricW <iemand@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 - <skeet@pobox.co m>
          Web site: http://www.pobox.com/~skeet
          Blog: http://www.msmvps.com/jon.skeet
          C# in Depth: http://csharpindepth.com

          Comment

          • EricW

            #6
            Re: String format strange behaviour

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

            Thanks, though!
            Eric



            "Jon Skeet [C# MVP]" <skeet@pobox.co mschreef in bericht
            news:MPG.233ad1 88ecad9ceef4a@m snews.microsoft .com...
            EricW <iemand@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 - <skeet@pobox.co m>
            Web site: http://www.pobox.com/~skeet
            Blog: http://www.msmvps.com/jon.skeet
            C# in Depth: http://csharpindepth.com

            Comment

            Working...