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
  1. Home
  2. General Programming
  3. C#
  4. how to take a table name from a text box!

how to take a table name from a text box!

Scheduled Pinned Locked Moved C#
oracletutorialquestion
15 Posts 4 Posters 0 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.
  • M mpvkrishnadhar

    hi i designed my form as textbox, button now in the buttonclick event i wrote a code which is as follows! try { string constr = "User Id = scott;Password=tiger;Provider=Msdaora.1"; OleDbConnection con = new OleDbConnection(constr); con.Open(); MessageBox.Show(" Connected to ORACLE!"); string q = "create table pert(enum number,ename varchar2(10),sal number)"; OleDbCommand cmd = new OleDbCommand(q, con); cmd.ExecuteNonQuery(); MessageBox.Show("Table Created!"); } catch (OleDbException a) { MessageBox.Show(a.Message); } now in the string q how do i add a textbox1.text so that while running my program i give the table name dynamically instead of statistically mentioning the table name as shown in the string q.

    A Offline
    A Offline
    AHSAN111
    wrote on last edited by
    #6

    Try the following:

    string q = "create table " + Textbox1.text + "(enum number,ename varchar2(10),sal number)";

    a better way (cleaner and easily readable) of doing it would be to use a local string variable to store the table name from the text box and concatenate the string using that variable.

    string temp = Textbox1.Text;
    string q = "create table "+temp+ " (enum number,ename varchar2(10),sal number)";

    L M 2 Replies Last reply
    0
    • A AHSAN111

      Try the following:

      string q = "create table " + Textbox1.text + "(enum number,ename varchar2(10),sal number)";

      a better way (cleaner and easily readable) of doing it would be to use a local string variable to store the table name from the text box and concatenate the string using that variable.

      string temp = Textbox1.Text;
      string q = "create table "+temp+ " (enum number,ename varchar2(10),sal number)";

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #7

      :thumbsdown::thumbsdown:

      Luc Pattyn [My Articles] Nil Volentibus Arduum

      A 1 Reply Last reply
      0
      • A AHSAN111

        Try the following:

        string q = "create table " + Textbox1.text + "(enum number,ename varchar2(10),sal number)";

        a better way (cleaner and easily readable) of doing it would be to use a local string variable to store the table name from the text box and concatenate the string using that variable.

        string temp = Textbox1.Text;
        string q = "create table "+temp+ " (enum number,ename varchar2(10),sal number)";

        M Offline
        M Offline
        mpvkrishnadhar
        wrote on last edited by
        #8

        hi AHSAN111! thanks for the reply mate! the first and the second parts that u explained are working fine for me! this is how my new code looks like! try { string constr = "User Id = scott;Password=tiger;Provider=Msdaora.1"; OleDbConnection con = new OleDbConnection(constr); con.Open(); MessageBox.Show(" Connected to ORACLE!"); string g = textBox1.Text; string q = "create table " +g+ "(enum number,ename varchar2(10),sal number)"; OleDbCommand cmd = new OleDbCommand(q, con); cmd.ExecuteNonQuery(); MessageBox.Show("Table Created!"); }

        L 1 Reply Last reply
        0
        • L Luc Pattyn

          :thumbsdown::thumbsdown:

          Luc Pattyn [My Articles] Nil Volentibus Arduum

          A Offline
          A Offline
          AHSAN111
          wrote on last edited by
          #9

          why did u downvote ?? please explain :)

          L 1 Reply Last reply
          0
          • M mpvkrishnadhar

            hi AHSAN111! thanks for the reply mate! the first and the second parts that u explained are working fine for me! this is how my new code looks like! try { string constr = "User Id = scott;Password=tiger;Provider=Msdaora.1"; OleDbConnection con = new OleDbConnection(constr); con.Open(); MessageBox.Show(" Connected to ORACLE!"); string g = textBox1.Text; string q = "create table " +g+ "(enum number,ename varchar2(10),sal number)"; OleDbCommand cmd = new OleDbCommand(q, con); cmd.ExecuteNonQuery(); MessageBox.Show("Table Created!"); }

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #10

            that is bad code, it is open for SQL injection, people can type anything they like in the TextBox and make your program execute it. I already told you precautions had to be taken against it, using a uneditable ComboBox rather than a TextBox is one way of doing just that. :)

            Luc Pattyn [My Articles] Nil Volentibus Arduum

            M 1 Reply Last reply
            0
            • L Luc Pattyn

              IMO the answer holds two parts: 1. don't use a TextBox, use a ComboBox (with ComboBoxStyle.DropDownList) presenting the allowable table names. 2. then perform string concatenation to build the SQL statement. :)

              Luc Pattyn [My Articles] Nil Volentibus Arduum

              A Offline
              A Offline
              AHSAN111
              wrote on last edited by
              #11

              the dropdown list would eliminate the facility of having tablenames according to user choices. This IMO is essential if we are dealing with a SQL Parser or a simillar application, or a situation where we need user-specified entity names in the database. We can use a textbox and employ reguler expressions / validation controls in order to eliminate the possibility of an incorrect tablename.

              L 1 Reply Last reply
              0
              • A AHSAN111

                why did u downvote ?? please explain :)

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #12

                I disliked very much however I did not downvote. Your code would not compile. And it is flawed, see my other posts in this thread. :|

                Luc Pattyn [My Articles] Nil Volentibus Arduum

                1 Reply Last reply
                0
                • A AHSAN111

                  the dropdown list would eliminate the facility of having tablenames according to user choices. This IMO is essential if we are dealing with a SQL Parser or a simillar application, or a situation where we need user-specified entity names in the database. We can use a textbox and employ reguler expressions / validation controls in order to eliminate the possibility of an incorrect tablename.

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #13

                  Something needs to be done to protect against abuse. Validation is one way, yes. :)

                  Luc Pattyn [My Articles] Nil Volentibus Arduum

                  1 Reply Last reply
                  0
                  • L Luc Pattyn

                    that is bad code, it is open for SQL injection, people can type anything they like in the TextBox and make your program execute it. I already told you precautions had to be taken against it, using a uneditable ComboBox rather than a TextBox is one way of doing just that. :)

                    Luc Pattyn [My Articles] Nil Volentibus Arduum

                    M Offline
                    M Offline
                    mpvkrishnadhar
                    wrote on last edited by
                    #14

                    ooh! but if i use comboboxstyle with allowable table names! the whole purpose of what i wanted would not be served! is there any way by which it can be done! ? but thank u very much for the suggestion mate! am seriously learning a lot from this!

                    A 1 Reply Last reply
                    0
                    • M mpvkrishnadhar

                      ooh! but if i use comboboxstyle with allowable table names! the whole purpose of what i wanted would not be served! is there any way by which it can be done! ? but thank u very much for the suggestion mate! am seriously learning a lot from this!

                      A Offline
                      A Offline
                      AHSAN111
                      wrote on last edited by
                      #15

                      As I said, you need to use a validator to ensure that only allowable table names are used to construct the query. You can use javascript + regex or .net validation controls as it suites you.

                      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