Reputation: 2329
I am trying to check if a record exists using javascript (I know is not the safest way to do it) but all of this is for internal use and safety is not an issue.
So I opened a recordset,
rs.Open("SELECT * FROM clie Where N_CLIENT =" + textbox1+ " AND C_POST_CLIENT = '" + textbox2+ "'",connection)
textbox1
and textbox2
are the values I am looking into the clie table, but first I need to check if the record exists. I tried assigning that rs.Open
to a variable and then compare it with something but it did not work
I tried using a RecordCount
but I kept getting -1. I read it was not intended for that, and that it should not be used for looking for records so there has to be another way to do this.
UPDATE _
Here is the whole function I am working on
function RecordExists(textfield1, textfield2)
{
var connection = new ActiveXObject("ADODB.Connection") ;
var connectionstring = "UID=admin;PWD=password";
connection.Open(connectionstring);
var rs = new ActiveXObject("ADODB.Recordset");
var textbox1= new String();
var textbox2=new String();
textbox1= document.getElementById(textfield1).value;
textbox2= document.getElementById(textfield2).value;
var isEmpty=new String();
rs.Open("SELECT count(*) as pers FROM clie HAVING N_CLIENT =" + textbox1+ " AND C_POST_CLIE = '" + textbox2+ "'",connection);
alert(rs.recordcount);
//alert(rs.fields(1));
//isEmpty = rs.Open("pers");
alert("Empty"+isEmpty);
if(pers=0)
alert("Record does not exist! pers="+pers);
else if(pers=1)
alert("Record exists! pers="+pers);
else
alert("not working");
rs.close;
connection.close;
}
}
Upvotes: 0
Views: 5482
Reputation: 12305
Try this:
rs.Open("SELECT count(1) as pers FROM clie Where N_CLIENT =" + textbox1+ " AND C_POST_CLIENT = '" + textbox2+ "'",connection)
You retrieve the pers field in this way:
perCounts = rs.('pers')
or
perCounts = rs.("pers")
Then if perCounts = 0 then user no exist....if 1 then user exist in your DB.
_______EDIT________________
function RecordExists(textfield1, textfield2)
{
var connection = new ActiveXObject("ADODB.Connection") ;
var connectionstring = "UID=admin;PWD=password";
connection.Open(connectionstring);
var rs = new ActiveXObject("ADODB.Recordset");
var textbox1= new String();
var textbox2=new String();
textbox1= document.getElementById(textfield1).value;
textbox2= document.getElementById(textfield2).value;
var isEmpty=new String();
rs.Open("SELECT count(*) as pers FROM clie HAVING N_CLIENT =" + textbox1+ " AND C_POST_CLIE = '" + textbox2+ "'",connection);
alert(rs.recordcount);
rs.MoveFirst();
perCounts = rs.Fields(0).Value;
if(perCounts=0)
alert("Record does not exist! pers="+pers);
else if(perCounts=1)
alert("Record exists! pers="+pers);
else
alert("not working");
rs.close;
connection.close;
}
}
Saludos.
Upvotes: 2
Reputation: 729
You should use the count method instead
rs.Open("SELECT count(*) FROM clie Where N_CLIENT =" + textbox1+ " AND C_POST_CLIENT = '" + textbox2+ "'",connection)
This will return the number of results
0 = 0 Clients 1 = 1 Client 2 = 2 Clients . . .
Upvotes: 0