By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,972 Members | 1,134 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,972 IT Pros & Developers. It's quick & easy.

Formatting TextBox Data

P: 63
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.
May 15 '07 #1
Share this Question
Share on Google+
18 Replies


kenobewan
Expert 2.5K+
P: 4,871
Please explain your problem and platform clearly. Thanks.
May 15 '07 #2

Plater
Expert 5K+
P: 7,872
Aside from watching out for single quotes, you should be fine?
(replace each instance of a single quote with two single quotes)
May 15 '07 #3

TRScheel
Expert 100+
P: 638
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
May 15 '07 #4

Frinavale
Expert Mod 5K+
P: 9,731
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"......
May 15 '07 #5

P: 63
sorry, the database data is actually displayed in a dataGridView

tried this...
Expand|Select|Wrap|Line Numbers
  1. Eval(queryTextDataGridViewTextBoxColumn).ToString().Replace(System.Environment .NewLine, "<br />"); 
  2.  
But VS2005 gives this error...
Expand|Select|Wrap|Line Numbers
  1. The name 'Eval' does not exist in the current context 
  2.  
May 17 '07 #6

Plater
Expert 5K+
P: 7,872
Eval is not a function in C#, it might be in VB, what language are you using?
May 17 '07 #7

Frinavale
Expert Mod 5K+
P: 9,731
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?
May 17 '07 #8

Plater
Expert 5K+
P: 7,872
Expand|Select|Wrap|Line Numbers
  1. Eval(queryTextDataGridViewTextBoxColumn).ToString().Replace(System.Environment .NewLine, "<br />"); 
  2.  
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 (DataGridViewCell) and use it's .Value property. So like
Expand|Select|Wrap|Line Numbers
  1. string celltextwithbr=mycell.Value.ToString().Replace(System.Environment .NewLine, "<br />"); 
  2.  
May 17 '07 #9

Frinavale
Expert Mod 5K+
P: 9,731
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 (DataGridViewCell) and use it's .Value property. So like
Expand|Select|Wrap|Line Numbers
  1. string celltextwithbr=mycell.Value.ToString().Replace(System.Environment .NewLine, "<br />"); 
  2.  

Yeah JavaScript has an Eval() function that does precisely that.
May 17 '07 #10

P: 63
thanks.
how do I set it to a certain column in the DataGridView control??

tried the below..
Expand|Select|Wrap|Line Numbers
  1. DataColumn dc = ((DataColumn)dataGridView1.Columns[9].DataGridView);
  2. string myform = dc.ToString().Replace(Environment.NewLine, "<br />");
  3.  
May 17 '07 #11

radcaesar
Expert 100+
P: 759
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.DataItem, "Name") %>
May 17 '07 #12

Plater
Expert 5K+
P: 7,872
You want the text in one particular cell of that column right?
Expand|Select|Wrap|Line Numbers
  1. DataGridViewColumn dc = dataGridView1.Columns[9];
  2. int whichrow = 0;//whichever row you want
  3. string mytext= dataGridView1.Rows[whichrow].Cells[dc.Index].Value.ToString();
  4. string myform = mytext.Replace(Environment.NewLine, "<br />");
  5. //and if you want to set that text BACK:
  6. //assuming the column type is a string
  7. dataGridView1.Rows[whichrow].Cells[dc.Index].Value=myform;
  8.  
May 17 '07 #13

P: 63
sorry,
every cell row of that column.

Thanks.
May 17 '07 #14

Plater
Expert 5K+
P: 7,872
Expand|Select|Wrap|Line Numbers
  1. DataGridViewColumn dc = dataGridView1.Columns[9];
  2. string mytext="";
  3. foreach(DataGridRow dr in dataGridView1.Rows)
  4. {
  5.    mytext= dr.Cells[dc.Index].Value.ToString();
  6.    //you may also chose to have a "<br/>" appended to the end of each row's item here
  7. }
  8. string myform = mytext.Replace(Environment.NewLine, "<br />");
  9.  
  10.  
May 17 '07 #15

P: 63
get error from VS2005 saying that DataGridRow is inaccessible due to its protection level??
May 17 '07 #16

Plater
Expert 5K+
P: 7,872
get error from VS2005 saying that DataGridRow is inaccessible due to its protection level??
Oops, DataGridViewRow not DataGridRow, but come on, try and do a little of your own leg work.
May 17 '07 #17

P: 63
I've been trying to get the below working...
Expand|Select|Wrap|Line Numbers
  1.             DataGridViewColumn dc = dataGridView1.Columns[9];
  2.             string mytext = "";
  3.             foreach (DataGridViewRow dr in dataGridView1.Rows)
  4.             {
  5.                 mytext = dr.Cells[dc.Index].Value.ToString();
  6.                 //you may also chose to have a "<br/>" appended to the end of each row's item here
  7.             }
  8.             string myform = mytext.Replace(Environment.NewLine, "<br />");
  9.  
where do I use the string myform?

thanks.
May 18 '07 #18

P: 63
changed WrapMode in DefaultCellStyle to "True"
and AutoSizeResMode in DataGridView Properties to "All Cells"

This seems to work.

Thanks.
May 18 '07 #19

Post your reply

Sign in to post your reply or Sign up for a free account.