Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

textbox to ntext sqlserver column 1

Status
Not open for further replies.

oakgrove

Programmer
Sep 22, 2002
51
US
I am trying to put the contents of a text box into an SQL server ntext field with an UPdate statement and parameters

<code>
...
cmdUpdate.commandtext = &quot;Update table SET comments=@comments&quot;
cmdUpdate.Parameters.Add(&quot;@comments&quot;,System.Data.SqlDbType.NText, 16).Value = txtcomments.Text
conn.Open()
cmdUpdate.ExecuteNonquery()
</code>

Nothing gets put into the field. What am I doing wrong?

TIA

Oakgrove Computer Grouper
Lansing, MI
 
Add this to your code:
Code:
dim objCon as SqlConnecion = New SqlConnection(&quot;your_connection_string&quot;)
....
cmdUpdate.Connection()=objCon
....
 
Sorry should have put more code. The actual update involves several txtboxes and columns that sucessfully update. On further testing I find that what is actually going on is that the update stops at the first cr/lf.
If the text box has
&quot;I am the first line
and I are the second.&quot;
The only thing that is put in the data column is:
&quot;I am the first line&quot;

Can anybody direct me to some way to deal with this. The users will want to put in cr/lf as well as empty lines.



Oakgrove Computer Grouper
Lansing, MI
 
Try adding this to all your textboxes
Code:
Server.HTMLEncode(textbox.Text.Replace(&quot;'&quot;,&quot;''&quot;))
 
oakgrove -

Do NOT do as Joulius suggests -- it will leave you open to a SQL injection attack (see
Regarding the NText column, you need to set the size in the Parameters.Add call to the size of the data you plan to insert:
Code:
cmdUpdate.Parameters.Add(&quot;@comments&quot;,System.Data.SqlDbType.NText, txtcomments.Text.Length).Value = txtcomments.Text
This is because SQL Server needs to know how much space to allocate in the database ahead of time (large blocks of data are sent to the database by the ADO layer in chunks -- the first chunk needs to have the total size in it).

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Sorry about that, oakgrove ... chiph is right here!
Here you go, buddy!
 
Thanks a bunch, you folks. Guess I was mixing up the issue of cr/lf with the size declaration.

Both your posts clarified the issues and fixed my problem



Oakgrove Computer Grouper
Lansing, MI
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top