Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. string replace method

string replace method

Scheduled Pinned Locked Moved C#
databasehelp
7 Posts 4 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ahawari09
    wrote on last edited by
    #1

    I use below code to fill the DataAdapter with a table contents, but the table name contain space such as "Table Name" I am using replace method to elemnate space from the table name to become "TableName" because when filling DataAdapter it returns an error and even with using the replace method I receive an error such "Syntax error in query. Incomplete query clause." private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) { DataSet1 ds = new DataSet1(); string stconn = "connection string"; OleDbConnection conn = new OleDbConnection(stconn); string deletespace = this.treeView1.SelectedNode.Text.ToString(); string Deletespace = deletespace.Replace(" ",""); OleDbCommand command = new OleDbCommand("select * from '" + Deletespace.ToString() + "'", conn); conn.Open(); OleDbDataAdapter Adapter = new OleDbDataAdapter(); Adapter.SelectCommand = command; Adapter.Fill(ds, Deletespace.ToString()); }

    hawari

    C 1 Reply Last reply
    0
    • A ahawari09

      I use below code to fill the DataAdapter with a table contents, but the table name contain space such as "Table Name" I am using replace method to elemnate space from the table name to become "TableName" because when filling DataAdapter it returns an error and even with using the replace method I receive an error such "Syntax error in query. Incomplete query clause." private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) { DataSet1 ds = new DataSet1(); string stconn = "connection string"; OleDbConnection conn = new OleDbConnection(stconn); string deletespace = this.treeView1.SelectedNode.Text.ToString(); string Deletespace = deletespace.Replace(" ",""); OleDbCommand command = new OleDbCommand("select * from '" + Deletespace.ToString() + "'", conn); conn.Open(); OleDbDataAdapter Adapter = new OleDbDataAdapter(); Adapter.SelectCommand = command; Adapter.Fill(ds, Deletespace.ToString()); }

      hawari

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Wow.

      ahawari09 wrote:

      string Deletespace = deletespace.Replace(" ","");

      Why create a new string ?

      ahawari09 wrote:

      Deletespace.ToString()

      What do you think 'ToString' does when called on a string ?

      ahawari09 wrote:

      ("select * from '" + Deletespace.ToString() + "'", conn);

      Why are you putting the table name in quotes ?

      ahawari09 wrote:

      conn.Open(); OleDbDataAdapter Adapter = new OleDbDataAdapter(); Adapter.SelectCommand = command; Adapter.Fill(ds, Deletespace.ToString());

      Why don't you close the connection ?

      Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

      A 1 Reply Last reply
      0
      • C Christian Graus

        Wow.

        ahawari09 wrote:

        string Deletespace = deletespace.Replace(" ","");

        Why create a new string ?

        ahawari09 wrote:

        Deletespace.ToString()

        What do you think 'ToString' does when called on a string ?

        ahawari09 wrote:

        ("select * from '" + Deletespace.ToString() + "'", conn);

        Why are you putting the table name in quotes ?

        ahawari09 wrote:

        conn.Open(); OleDbDataAdapter Adapter = new OleDbDataAdapter(); Adapter.SelectCommand = command; Adapter.Fill(ds, Deletespace.ToString());

        Why don't you close the connection ?

        Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

        A Offline
        A Offline
        ahawari09
        wrote on last edited by
        #3

        I am using treeview control to display table contents and when I duble click the selected node I display a spacific table contents in a listview control, to do that I have to pass table name which is the selected node.text to the select statment then to the DataAdapter. There is one problem table names should not contain space or it will not run without an error. So I am tring to remove space from node.text.

        hawari

        C 1 Reply Last reply
        0
        • A ahawari09

          I am using treeview control to display table contents and when I duble click the selected node I display a spacific table contents in a listview control, to do that I have to pass table name which is the selected node.text to the select statment then to the DataAdapter. There is one problem table names should not contain space or it will not run without an error. So I am tring to remove space from node.text.

          hawari

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          Wow. OK, this is a total disaster, not in least because you plainly have no idea. Buy a book on SQL and read it, please. A table name NEVER goes in quotes, this is the source of your error. If a table name has spaces in it, in the database, you use [] to reference it, as in [my table with a stupid name]. I'd never use spaces to startwith, but leading and trailing spaces ( which you can remove with the Trim method if you like ) are not doing to stop the code from working.

          Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

          A 1 Reply Last reply
          0
          • C Christian Graus

            Wow. OK, this is a total disaster, not in least because you plainly have no idea. Buy a book on SQL and read it, please. A table name NEVER goes in quotes, this is the source of your error. If a table name has spaces in it, in the database, you use [] to reference it, as in [my table with a stupid name]. I'd never use spaces to startwith, but leading and trailing spaces ( which you can remove with the Trim method if you like ) are not doing to stop the code from working.

            Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

            A Offline
            A Offline
            ahawari09
            wrote on last edited by
            #5

            Wow. I belive you are the one who has no idea, because you did not realy understand the problem. First of all I am not working with SQL server to produce the code I sent to you, I am working with C#. That is why you can see embedded SQL "with a stupid name" table goes in quotes. Thank you and please you don't have to reply to any of my questions. Regards,

            hawari

            D P 2 Replies Last reply
            0
            • A ahawari09

              Wow. I belive you are the one who has no idea, because you did not realy understand the problem. First of all I am not working with SQL server to produce the code I sent to you, I am working with C#. That is why you can see embedded SQL "with a stupid name" table goes in quotes. Thank you and please you don't have to reply to any of my questions. Regards,

              hawari

              D Offline
              D Offline
              darkelv
              wrote on last edited by
              #6

              :omg: By the way, you don't have to use .Text for the table name. There's a tag of object type where you can "tag" the table name to, or in more complex case, you can tag a class object that contains the table name plus other information, ie the whole select statement, for example. By, by the way, table name in SQL statement should not be enclosed by single quotes.

              1 Reply Last reply
              0
              • A ahawari09

                Wow. I belive you are the one who has no idea, because you did not realy understand the problem. First of all I am not working with SQL server to produce the code I sent to you, I am working with C#. That is why you can see embedded SQL "with a stupid name" table goes in quotes. Thank you and please you don't have to reply to any of my questions. Regards,

                hawari

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                No, he's correct.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups