Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Jscript variable scope 2

Status
Not open for further replies.

EnemyGateIsDown

Technical User
Oct 3, 2002
235
GB
Hi Guys,

Im relatively new to Jscript and im having some scoping fun..

I am retrieving a record from a recordset using the following code:


//Create Recordset

var rsFielddefs = Server.CreateObject("ADODB.Recordset");

var SQLFielddefs = "Select * from [Field Definitions]"

//Call Sql

rsFielddefs.Open(SQLFielddefs,Conn);


rsFielddefs.MoveFirst();

strFind = "Name = '" +sfieldname+ "'"
rsFielddefs.Find(strFind);

// = "Name = '" +sfieldname+ "'";
//rsFielddefs.Filter = filter;

sFieldDefID = rsFielddefs("FieldDefs_ID");

rsFielddefs.Close;

Response.Write ("FieldDefID is: " + sFieldDefID); //+ "Location_ID is: "+sLocation_id+"<br>");
Response.end



However the response.write returns nothing for the variable. If however I move the response write above the rsFielddefs.Close it returns the correct value.

I need the variable that I assign to be available outside of the recordset code.

I would be grateful if someone could point me in the right direction.

Thanks,

Chris
 
Rather than waste time on using the Find method of the recordset, you should build your SQL statement to handle that for you. ie.
"Select * from [Field Definitions] WHERE myFieldname='" & strFind & "'"

Then all you really need to do is test for EOF. If you're EOF then you have to results. IF not rsFielddefs.EOF then assign the field value to a variable and do whatever you need to do with it.
 
Sorry Chaps, more readable version below, just found the code tag :)

Hi Guys,

Im relatively new to Jscript and im having some scoping fun..

I am retrieving a record from a recordset using the following code:

Code:
//Create Recordset

                var rsFielddefs = Server.CreateObject("ADODB.Recordset");

                var SQLFielddefs = "Select * from [Field Definitions]"

                //Call Sql

                rsFielddefs.Open(SQLFielddefs,Conn);


                  rsFielddefs.MoveFirst();

                   strFind = "Name = '" +sfieldname+ "'"
                rsFielddefs.Find(strFind);

                // = "Name = '" +sfieldname+ "'";
                //rsFielddefs.Filter = filter;

                sFieldDefID = rsFielddefs("FieldDefs_ID");

                rsFielddefs.Close;

                Response.Write ("FieldDefID is: " + sFieldDefID); //+ "Location_ID is: "+sLocation_id+"<br>");
                Response.end

However the response.write returns nothing for the variable. If however I move the response write above the rsFielddefs.Close it returns the correct value.

I need the variable that I assign to be available outside of the recordset code.

I would be grateful if someone could point me in the right direction.

Thanks,

Chris
 
Yeah I appreciate I could add the constraint to the SQL statement instead of using the find method, but my problem really lies with the scope of the FieldDefID variable..

Any ideas why it should be out of scope below the recordset.close?

Thanks

Chris
 
The behavior you are experiencing is pretty bizzare. As a general rule I always assign my field values to variables, close the recordset (and connection if I can) and then do what I need to do with them. The scope of the variable should be global unless this is part of a sub or function.
 
hmmm.. have resolved it now by closing the recordset after I use the variable, which goes against the policy of closing the recordset as soon as it is not needed but it works...

I was reading around about Jscript scope rules and found that there is class level scope and am wondering if that has anything to do with it.. ho hum, i'll go with it for now and revise the code when I find a better solution..

Thanks for your help Veep.

Chris
 
If you come up with an explanation please post it. Thanks.
 
EnemyGateIsDown,

this line:
Code:
sFieldDefID = rsFielddefs("FieldDefs_ID");
is assigning an object to the variable sFieldDefID. I'm pretty sure if you did a typeOf on sFieldDefID you'd get [object]. My guess is that once you're closing the connection it's causing the object to not exist anymore. And in turn, it's assigning the value inside the object to the variable. If you'd like to access the value before closing the object, simply change it to this:
Code:
sFieldDefID = rsFielddefs("FieldDefs_ID")[b].value[/b];
and that should solve your problem.


-kaht

banghead.gif
 
Thanks Kaht (and Veep for earlier assistance), That was the problem exactly, it makes sense that because I was assigning from the Object not its value, when the object was closed so the variable which was of the type of the object would go out of scope too.. by using the value instead the variable stays in scope..

Thanks again to both of you.

Chris



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top