Formatting TextBox Data

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • obrienkev
    New Member
    • May 2007
    • 63

    Formatting TextBox Data

    Hi,

    I have a multiline TextBox. Text contained in the TextBox will be stored in a SQL Server Database.
    How do I format the textBox correctly for database entry?
    e.g. How do I ensure that new lines will be added, etc?

    Thanks.
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Please explain your problem and platform clearly. Thanks.

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      Aside from watching out for single quotes, you should be fine?
      (replace each instance of a single quote with two single quotes)

      Comment

      • TRScheel
        Recognized Expert Contributor
        • Apr 2007
        • 638

        #4
        Originally posted by Plater
        Aside from watching out for single quotes, you should be fine?
        (replace each instance of a single quote with two single quotes)
        And watch for sql injections

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Originally posted by obrienkev
          Hi,

          I have a multiline TextBox. Text contained in the TextBox will be stored in a SQL Server Database.
          How do I format the textBox correctly for database entry?
          e.g. How do I ensure that new lines will be added, etc?

          Thanks.
          You have to screen the text in the TextBox.
          You'll have to grab it and filter it to make sure that it is formatted the way that your database expects (eg. is less than the max length). It is also Strongly advised that you screen for malicious database commands that may be executed while running database queries at later dates. You should look for "Drop Table", "Delete", "Select", "Truncate"..... .

          Comment

          • obrienkev
            New Member
            • May 2007
            • 63

            #6
            sorry, the database data is actually displayed in a dataGridView

            tried this...
            Code:
            Eval(queryTextDataGridViewTextBoxColumn).ToString().Replace(System.Environment .NewLine, "<br />");
            But VS2005 gives this error...
            Code:
            The name 'Eval' does not exist in the current context

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              Eval is not a function in C#, it might be in VB, what language are you using?

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                Originally posted by Plater
                Eval is not a function in C#, it might be in VB, what language are you using?
                I've never seen it in VB either.
                Maybe its a custom function?

                Comment

                • Plater
                  Recognized Expert Expert
                  • Apr 2007
                  • 7872

                  #9
                  Originally posted by obrienkev
                  Code:
                  Eval(queryTextDataGridViewTextBoxColumn).ToString().Replace(System.Environment .NewLine, "<br />");
                  Best I can tell Eval() is used in other languages to take a string and do operations on it, like Eval("1+2") would give you 3. Not sure what he is attempting to do there with it?

                  If you want the text from a field, get to the correct Cell (DataGridViewCe ll) and use it's .Value property. So like
                  [code=C#]
                  string celltextwithbr= mycell.Value.To String().Replac e(System.Enviro nment .NewLine, "<br />");
                  [/code]

                  Comment

                  • Frinavale
                    Recognized Expert Expert
                    • Oct 2006
                    • 9749

                    #10
                    Originally posted by Plater
                    Best I can tell Eval() is used in other languages to take a string and do operations on it, like Eval("1+2") would give you 3. Not sure what he is attempting to do there with it?

                    If you want the text from a field, get to the correct Cell (DataGridViewCe ll) and use it's .Value property. So like
                    [code=C#]
                    string celltextwithbr= mycell.Value.To String().Replac e(System.Enviro nment .NewLine, "<br />");
                    [/code]

                    Yeah JavaScript has an Eval() function that does precisely that.

                    Comment

                    • obrienkev
                      New Member
                      • May 2007
                      • 63

                      #11
                      thanks.
                      how do I set it to a certain column in the DataGridView control??

                      tried the below..
                      Code:
                      DataColumn dc = ((DataColumn)dataGridView1.Columns[9].DataGridView);
                      string myform = dc.ToString().Replace(Environment.NewLine, "<br />");

                      Comment

                      • radcaesar
                        Recognized Expert Contributor
                        • Sep 2006
                        • 759

                        #12
                        Originally posted by Frinavale
                        Yeah JavaScript has an Eval() function that does precisely that.
                        Hey Frin,
                        Its in .NET, We can use it in while we code In-Line

                        Like This,

                        <%# DataBinder.Eval (Container.Data Item, "Name") %>

                        Comment

                        • Plater
                          Recognized Expert Expert
                          • Apr 2007
                          • 7872

                          #13
                          You want the text in one particular cell of that column right?
                          Code:
                          DataGridViewColumn dc = dataGridView1.Columns[9];
                          int whichrow = 0;//whichever row you want
                          string mytext= dataGridView1.Rows[whichrow].Cells[dc.Index].Value.ToString();
                          string myform = mytext.Replace(Environment.NewLine, "<br />");
                          //and if you want to set that text BACK:
                          //assuming the column type is a string
                          dataGridView1.Rows[whichrow].Cells[dc.Index].Value=myform;

                          Comment

                          • obrienkev
                            New Member
                            • May 2007
                            • 63

                            #14
                            sorry,
                            every cell row of that column.

                            Thanks.

                            Comment

                            • Plater
                              Recognized Expert Expert
                              • Apr 2007
                              • 7872

                              #15
                              Code:
                              DataGridViewColumn dc = dataGridView1.Columns[9];
                              string mytext="";
                              foreach(DataGridRow dr in dataGridView1.Rows)
                              {
                                 mytext= dr.Cells[dc.Index].Value.ToString();
                                 //you may also chose to have a "<br/>" appended to the end of each row's item here
                              }
                              string myform = mytext.Replace(Environment.NewLine, "<br />");

                              Comment

                              Working...