Wrox Programmer Forums
|
Classic ASP Databases Discuss using ASP 3 to work with data in databases, including ASP Database Setup issues from the old P2P forum on this specific subtopic. See also the book forum Beginning ASP.NET Databases for questions specific to that book. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 9th, 2008, 06:31 AM
Registered User
 
Join Date: Sep 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to update using ADODB

Hi All

Generally we are using the following code to update using ADODB

sql = "SELECT * FROM customers WHERE CustomerID=" + custID;
recordSet.Open(sql, connection, 1, 2);
    recordSet("FirstName") = firstName;
    recordSet("MiddleInitial") = middleInitial;
recordSet.Update();
recordSet.Close();
But, we are facing sql injection problem. so what we can't pass value directly to query. So I have changed to
var sqlcmd = Server.CreateObject("ADODB.Command");
                        sqlcmd.CommandText = sql;
                        sqlcmd.CommandType = 1;
                        sqlcmd.Parameters.Append(sqlcmd.CreateParameter("@ column1",200,1 ,10,custID));

My Doubt is, How to update sqlcmd using recordset. can you please explain?

 
Old September 9th, 2008, 06:07 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

First of all, to fix your SQL Injection problems with your original code, you could just do this:

Code:
<%
...
custid = parseInt(custid); // ensure it really *IS* an integer
if ( isNaN(custid) )
{
    Response.Redirect( "badCustomerId.html" ); // or wherever
    Response.End( );
}
sql = "SELECT * FROM customers WHERE CustomerID=" + custid;
...
%>
***********************

But to do it the other way:
Code:
<%
...
var sql = "UPDATE customers SET FirstName=@fname, LastName=@lname WHERE CustomerId=@custid"
var sqlcmd =  Server.CreateObject("ADODB.Command");
sqlcmd.CommandText = sql;
sqlcmd.CommandType = 1;
sqlcmd.Parameters.Append(sqlcmd.CreateParameter("@fname",200,1 ,100,firstName));
sqlcmd.Parameters.Append(sqlcmd.CreateParameter("@lname",200,1 ,100,lastName));
sqlcmd.Parameters.Append(sqlcmd.CreateParameter("@custid",3,1,,custID));
sqlcmd.Execute();
...
%>
That's if you are using SQL Server. If you are using Access, I *believe* you would need to use

var sql = "UPDATE customers SET FirstName=?, LastName=? WHERE CustomerId=?"

***************************

FINALLY...

You *COULD* do it the simplest way of all, you know:
Code:
<%
...
var sql = "UPDATE customers SET "
        + " FirstName='" + firstName.replace(/\'/g,"''") + "',"
        + " LastName='" + lastName.replace(/\'/g,"''") + "' "
        + " WHERE CustomerId=" + parseInt(custID);
connection.Execute( sql );
...
%>





Similar Threads
Thread Thread Starter Forum Replies Last Post
ADODB Recordset sporkman43 Classic ASP Basics 4 November 9th, 2006 04:51 AM
Can we Inherit ADODB venki5star Visual Studio 2005 3 March 13th, 2006 10:50 AM
ADODB.Fields EricJ ASP.NET 1.x and 2.0 Application Design 6 May 5th, 2005 07:21 AM
How to get adodb.record from adodb.recordset John Pennington Pro VB Databases 1 November 20th, 2004 06:17 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.