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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Handling 0 search results 1

Status
Not open for further replies.

david7777777777

Programmer
Sep 26, 2001
417
US
Can someone point me in the right direction for writing a script that handles the situation where a search returns no results?

Talking to a SQL 2000 database, I have an ASP page that contains a recordset, a textbox, and a grid DTC. The recordset gets its search criteria from what the user types in the textbox. If there are no search results returned I'd like a message to display informing the user to try again or something like that.

Thanks.
 
Where do you want this message displayed?

Displaying it on the same page is easy, you just check if your recordset is empty.

Put this somwhere you want your message displayed (your rs must be already opened),

<% if rs.BOF and rs.EOF then
response.write &quot;<P>Try again...</P>&quot;
end if %>

If your recordset isn't open just put another if statement before you check if your recordset is empty
if rs.isOpen() then...
end if

If you'll put this in code (without <%%>), the message will be displayed on top of the page.

hope this is what you want.

&quot;Defeat is not the worst of failures. Not to have tried is the true failure.&quot;
-George E. Woodberry
 
I've got a button on the page that opens the recordset once the user has typed the search criteria in a text box, here's the code for the button

Sub btnGo_onclick()
rsEmployees.open
btnGo.hide
lstDepartments.hide
End Sub

Should I put this code you gave me within the code for this button or put it elsewhere?
 

If you put the code within the code for your button, the message will display on top of your page.

Sub btnGo_onclick()
rsEmployees.open
if rs.BOF and rs.EOF then
response.write &quot;<P>Try again...</P>&quot;
else
btnGo.hide
lstDepartments.hide
end if
End Sub

if you want the messege to display somwhere else put the code between <BODY> tags, where you want your message to display.

<% if rs.isOpen() then
if rs.BOF and rs.EOF then
response.write &quot;<P>Try again...</P>&quot;
btnGo.show
lstDepartments.show
end if
end if %>




&quot;Defeat is not the worst of failures. Not to have tried is the true failure.&quot;
-George E. Woodberry
 
It is quite easy to adjust the Grid DTC code to print a message when the recordset is empty. You can even add a property to the grid to specify the required text - though you will have to remember this name, as it will not pop-up in the VI editor. I will provide the code if you are interested.

You could then say:

grdMyGrid.emptyRecordsetMessage = &quot;Nothing to display&quot;
(Content Management)
 
Open the _ScriptLibrary/DATAGRID.ASP

Add a Grid property...

function _DataGrid(strName,objParent)
{
// public members
this.id = strName;
this.name = strName;

//Message if the recordset is closed or empty
// (no grid is drawn)
this.strMessageWhenEmpty = '';


then adjust the display method (watch where you add the curly braces!)...

function _DG_display(bReturnText)
{
var strHTML = '';
if (this._bVisible)
{
var objRS = this._objDataSource;
if (!(objRS != null && !objRS._isEmpty())) {
// MessageWhenEmpty feature
//recordset is null or empty
if (this.strMessageWhenEmpty != '') {
strHTML = this.strMessageWhenEmpty;
}
}
else
{

...rest of the display routine...
}
}
this._preserveState();
if (bReturnText != true)
..etc..


On your ASP page...
grdGrid.strMessageWhenEmpty = &quot;<B>Empty.</B>&quot;


The grid will forget the message between server round-trips, so always set the message text on page load - or add more code to the Grid DTC - this time to the 'preserveState()/restoreState()' methods:

function _DG__preserveState()
{
if (this.maintainState)
{ // preserve state in hidden field
var state = new Object;
state._bVisible = this._bVisible;
if (this.allColumns)
state.allColumns = true;
//save MessageWhenEmpty feature
if (this.strMessageWhenEmpty != '')
state.strMessageWhenEmpty = this.strMessageWhenEmpty;

...etc...


function _DG__restoreState()
{
var r = false;
this._fireEvent(this._funcInit);
if (this.maintainState)
{
var state = thisPage.unpersistState(this.name);
if (state != null)
{
//restore MessageWhenEmpty feature
if (state.strMessageWhenEmpty != null)
this.strMessageWhenEmpty
= state.strMessageWhenEmpty;

...etc... (Content Management)
 
I cannot thank you enough, but is this partial code? I mean I don't know nearly enough about programming to finish the code you've started here. I can usually figure out where to adapt object names to my specific application, and I can usually tweak little bits of code here and there, but I'm not comfortable with this amount of unfinished code. Can you bring me a little closer to complete code please? Thank you very much Merlin.
 
Thats it I'm afraid. I could add in some comments or the use-stylesheet property if you wish (so you can set the grid colouring etc. in a stylesheet - for consistency).

JavaScript is a bit of an odd beast. In this case we appear to be inventing a 'property' on the fly - without even declaring a variable to hold it. It is initially set to an empty string, then later used in the display routine - its as simple as that. The 'this' always refers to the current instance - which is very handy, except that VI editor does not recognise it in order to pop-up methods/properties of the current object.

The preserve/restore State methods already exist in many of the DTC code modules. These map onto the 'view state' feature found in .Net controls. Indeed, the ideas used in DTC's and the SOM are very much present in ASP.Net. (Content Management)
 
Merlin,
Do I need a closing curly bracket right after this part of the code you gave me?

//Message if the recordset is closed or empty
// (no grid is drawn)
this.strMessageWhenEmpty = '';


(watch where you add the curly braces!)...
 
No. The place that needs curly braces is in the Display method - because you are adding an if clause that spans most of the code! If you were wanting to create 'clean' code, you may want to split the display routine into a number of smaller functions. (Content Management)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top