0

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;
    }
}
3
  • Are you trying to query a backend SQL database using Node? or the WebSQL database that some browsers support? Commented Feb 27, 2013 at 14:50
  • Is this an html page or is there some sort of server side application code such as .net, php, coldfusion, etc available? By the way, security is not the only reason to do it better than you are currently attempting. An apostrophe will probably break your query as you have it currently written. Commented Feb 27, 2013 at 14:57
  • @DanBracuk it might be, but what I'm doing is basic logic algorithms and I haven't encountered that problem so far. I hope I never will, but thanks for the advice, sadly this is the way it has to be done Javascript and SQL, it is not up to me. Commented Feb 27, 2013 at 15:25

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

8 Comments

It's not working. So let me elaborate more, the record might exist with the values of textbox1 and txtbox2 together, eg: Smith and 1234, but if Smith and 5678 does not exist it should return 0 for pers. Does this make sense?
if Smith and 1234 exist returns pers=1, but if Smith and 5678 does not exist it should return 0 for pers.....thats correct
Yeah but it is returning pers=1 for both
I think i understand.....you are retrieving the pers field in the wrong way....let me update my answer
It gives me an error, rs. needs a function after. A parenthesis after the period is illegal. I tried rs.Open("pers") but it tells me the connection is already open
|
0

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 . . .

1 Comment

How does it return it? I tried assigning a variable to rs.Open(....) but it didn't return anything but -1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.