473,387 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Handling quotes in a textbox?

REB
I have a button that executes this sql query:
sqlcmdInsertPhysicalNote.CommandText = "INSERT INTO DriverPhysicalNotes
(DriverInformationID, PhysicalNoteDate, PhysicalNote) VALUES ('" +
ddlDriverName.SelectedValue + "','" + DateTime.Now.ToShortDateString() +
"','" + txtNotes.Text + "')";

I am having a problem when a user enters an apostrophe ' into the textbox it
cause the save to error out. THis is the error

System.Data.SqlClient.SqlException: Unclosed quotation mark before the
character string 'Test in a quote')'. Line 1: Incorrect syntax near 'Test in
a quote')'. at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at
DOT.Driver.btnAddPhysicalNote_Click(Object sender, EventArgs e)

How can I keep this from happening? Quotes in the notes section should be
allowed.

Thanks,

REB
Nov 18 '05 #1
3 2953
Hi, REB,

You should use parameters in order to avoid such errors and exceptions. See
the example in the article:

http://msdn.microsoft.com/library/en...etersTopic.asp

Greetings
Martin
"REB" <ru**@bjorgaard.com> wrote in message
news:#H*************@TK2MSFTNGP12.phx.gbl...
I have a button that executes this sql query:
sqlcmdInsertPhysicalNote.CommandText = "INSERT INTO DriverPhysicalNotes
(DriverInformationID, PhysicalNoteDate, PhysicalNote) VALUES ('" +
ddlDriverName.SelectedValue + "','" + DateTime.Now.ToShortDateString() +
"','" + txtNotes.Text + "')";

I am having a problem when a user enters an apostrophe ' into the textbox it cause the save to error out. THis is the error

System.Data.SqlClient.SqlException: Unclosed quotation mark before the
character string 'Test in a quote')'. Line 1: Incorrect syntax near 'Test in a quote')'. at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at
DOT.Driver.btnAddPhysicalNote_Click(Object sender, EventArgs e)

How can I keep this from happening? Quotes in the notes section should be
allowed.

Thanks,

REB

Nov 18 '05 #2
Use SqlParameter

Tu-Thac

----- REB wrote: ----

I have a button that executes this sql query
sqlcmdInsertPhysicalNote.CommandText = "INSERT INTO DriverPhysicalNote
(DriverInformationID, PhysicalNoteDate, PhysicalNote) VALUES ('"
ddlDriverName.SelectedValue + "','" + DateTime.Now.ToShortDateString()
"','" + txtNotes.Text + "')"

I am having a problem when a user enters an apostrophe ' into the textbox i
cause the save to error out. THis is the erro

System.Data.SqlClient.SqlException: Unclosed quotation mark before th
character string 'Test in a quote')'. Line 1: Incorrect syntax near 'Test i
a quote')'. at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() a
DOT.Driver.btnAddPhysicalNote_Click(Object sender, EventArgs e

How can I keep this from happening? Quotes in the notes section should b
allowed

Thanks

RE

Nov 18 '05 #3
As others have posted, you would better to use an SQLParamter, but if
you "must" do this, you need to "escape" the single quote by doubling
it. use

replace(txtNotes.Text, "'", "''")

The second parameter in that is a single quote surrounded by double
quotes. The second parameter is Two Single Quotes surrounded by double
quotes. Keep in mind that there are other characters that can cause
problems too, but the single quote thing is definintely the most
common.

On Mon, 1 Mar 2004 09:42:59 -0600, "REB" <ru**@bjorgaard.com> wrote:
I have a button that executes this sql query:
sqlcmdInsertPhysicalNote.CommandText = "INSERT INTO DriverPhysicalNotes
(DriverInformationID, PhysicalNoteDate, PhysicalNote) VALUES ('" +
ddlDriverName.SelectedValue + "','" + DateTime.Now.ToShortDateString() +
"','" + txtNotes.Text + "')";

I am having a problem when a user enters an apostrophe ' into the textbox it
cause the save to error out. THis is the error

System.Data.SqlClient.SqlException: Unclosed quotation mark before the
character string 'Test in a quote')'. Line 1: Incorrect syntax near 'Test in
a quote')'. at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at
DOT.Driver.btnAddPhysicalNote_Click(Object sender, EventArgs e)

How can I keep this from happening? Quotes in the notes section should be
allowed.

Thanks,

REB


Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Option^Explicit | last post by:
What I'm trying to do: Open a text file and display the contents in a text box (I've done this) Need to be able to edit the file from within the textbox and have it save back to the source...
10
by: David | last post by:
Hello I am trying to collect errors and record them in a table instead of a popup message stopping my code. It seems to work ok, but when I try to add ERR.Description to my code it fails on Syntax...
6
by: SStory | last post by:
How can I handle the user entering single quotes like in Bob's mini mart? If I use command objects will this no longer be an issue? I guess that would mean no simple adhoc SQL statements...
3
by: Justin Morris via DotNetMonster.com | last post by:
<asp:TextBox ID="TextBox1" runat="server" value='<%=Server.HtmlEncode (Request.Cookies("Username")("Username"))%>'/> <input name="Password" type="text" id="Password" value='<%...
7
by: Itar | last post by:
I'm having a problem supressing characters in a text box. I only want alpha numeric characters (no special chars). I can handle the TextBox_KeyPress event to supress the invalid characters when...
2
by: Agnes | last post by:
When the textbox 's Enabled = True, (its backcolor is white) When the Textbox got focus (backcolor change to pink by my code) when the textbox lost focus(back color change to white by my code)...
1
by: jhoff | last post by:
I'm trying to use javascript to execute code when the enter key is pressed in a text box. Basically, I'm doing this... <html> <body onload="load()" onunload="GUnload()"> <form name=zipform>...
2
by: Jordan | last post by:
I need to handle UI events in a worker thread instead of the primary UI thread. In C#, is the normal UI event handling behavior to run in a context thread on the thread pool or are events always...
1
by: Al G | last post by:
Hi, I am reading a CSV file with the code below. When I run into a " (quote mark) in the file, I get a "malformed line" exception. Can anyone point me to a way to handle this? Thanks in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.