Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > sinlge quotes replace problem

Reply
Thread Tools

sinlge quotes replace problem

 
 
Roy Adams
Guest
Posts: n/a
 
      05-31-2004
Hi group I'm having trouble using the replace command
Here's my code below

<%@LANGUAGE="JAVASCRIPT" CODEPAGE="1252"%>
<!--#include file="../../Connections/conn.asp" -->

<%


if( String(Request.Form("ProductName")) != "undefined" ){//formfield
is not empty
var NavID = 1;

var ProductName = String(Request.Form("ProductName"));
var Price = String(Request.Form("Price"));
var Descript = String(Request.Form("Description"));
var ProductCode = String(Request.Form("ProductCode"));
//get the form fields and put into vars
var TableFields = "ProductName,Price,Description,NavID,ProductCo de";

var FormFields = "'" + ProductName + "','" + Price + "','" + Descript
+ "','" + NavID + "','" + ProductCode+"'" ;

/// it works ok if i remove the replace
FormFields=FormFields.replace("'", "''");


conn = Server.CreateObject('ADODB.Command');

conn.ActiveConnection = conn_STRING;

conn.CommandText = ("insert into products ("+ TableFields +") values
('" + FormFields + ")" );


conn.Execute();
conn.ActiveConnection.Close();

}

%>

any suggestions?
 
Reply With Quote
 
 
 
 
Slim
Guest
Posts: n/a
 
      05-31-2004

"Roy Adams" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Hi group I'm having trouble using the replace command
> Here's my code below
>
> <%@LANGUAGE="JAVASCRIPT" CODEPAGE="1252"%>
> <!--#include file="../../Connections/conn.asp" -->
>
> <%
>
>
> if( String(Request.Form("ProductName")) != "undefined" ){//formfield
> is not empty
> var NavID = 1;
>
> var ProductName = String(Request.Form("ProductName"));
> var Price = String(Request.Form("Price"));
> var Descript = String(Request.Form("Description"));
> var ProductCode = String(Request.Form("ProductCode"));
> //get the form fields and put into vars
> var TableFields = "ProductName,Price,Description,NavID,ProductCo de";
>
> var FormFields = "'" + ProductName + "','" + Price + "','" + Descript
> + "','" + NavID + "','" + ProductCode+"'" ;
>
> /// it works ok if i remove the replace
> FormFields=FormFields.replace("'", "''");


try

FormFields=replace(FormFields,"'", "''");



>
>
> conn = Server.CreateObject('ADODB.Command');
>
> conn.ActiveConnection = conn_STRING;
>
> conn.CommandText = ("insert into products ("+ TableFields +") values
> ('" + FormFields + ")" );
>
>
> conn.Execute();
> conn.ActiveConnection.Close();
>
> }
>
> %>
>
> any suggestions?



 
Reply With Quote
 
 
 
 
TomB
Guest
Posts: n/a
 
      05-31-2004
This...
conn.CommandText = ("insert into products ("+ TableFields +") values
('" + FormFields + ")" );

Looks like it has an apostrophe right after the opening bracket.
You've replaced all of your form field delimiters with double apostrophes.
So your statement is going to look like.....
insert into products (ProductName,Price,Description,NavID,ProductCode)
values
(''productname'',''price'',''description'',''navid '',''productcode'')

I believe what you want to do is.....

Price.replace("'","''")
ProductName.Replace("'","''")
//ETC..

var FormFields = "'" + ProductName + "','" + Price + "','" + Descript
+ "','" + NavID + "','" + ProductCode+"'" ;

So that your result will look like
insert into products (ProductName,Price,Description,NavID,ProductCode)
values
('product''sname',price,'description',navid,'produ ctcode')

Note the double apostrophe after the t in productsname. This will insert
product'sname into the ProductName field.
You'll also note that price has no apostrophes as I'm assuming that's a
number field.



"Roy Adams" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Hi group I'm having trouble using the replace command
> Here's my code below
>
> <%@LANGUAGE="JAVASCRIPT" CODEPAGE="1252"%>
> <!--#include file="../../Connections/conn.asp" -->
>
> <%
>
>
> if( String(Request.Form("ProductName")) != "undefined" ){//formfield
> is not empty
> var NavID = 1;
>
> var ProductName = String(Request.Form("ProductName"));
> var Price = String(Request.Form("Price"));
> var Descript = String(Request.Form("Description"));
> var ProductCode = String(Request.Form("ProductCode"));
> //get the form fields and put into vars
> var TableFields = "ProductName,Price,Description,NavID,ProductCo de";
>
> var FormFields = "'" + ProductName + "','" + Price + "','" + Descript
> + "','" + NavID + "','" + ProductCode+"'" ;
>
> /// it works ok if i remove the replace
> FormFields=FormFields.replace("'", "''");
>
>
> conn = Server.CreateObject('ADODB.Command');
>
> conn.ActiveConnection = conn_STRING;
>
> conn.CommandText = ("insert into products ("+ TableFields +") values
> ('" + FormFields + ")" );
>
>
> conn.Execute();
> conn.ActiveConnection.Close();
>
> }
>
> %>
>
> any suggestions?



 
Reply With Quote
 
Roy
Guest
Posts: n/a
 
      05-31-2004
Hi TomB
thanks for your help, you were exactly right,
but after i did that i found that it worked for the first single quote
but found now if a user inputs more than one single or double quote into
the text field it threw up errors again so i tried
ProductName=ProductName.replace(/'/g, "''");
which worked!!
cheers

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
regex to avoid escaped quotes or double quotes jOhn Ruby 1 01-29-2008 08:31 PM
PHP double quotes inside double quotes MSB Computer Support 11 10-21-2006 01:09 PM
Asp.NET Javascript string, want to pass '(single quotes' within '(single quotes) Chris ASP .Net 1 03-24-2006 09:03 PM
Quotes/Double Quotes in Image Control Chris White ASP .Net 1 09-22-2004 06:22 AM
Multiline quotes - escaping quotes - et al Lawrence Tierney Java 3 12-24-2003 05:12 PM



Advertisments