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

Return to search results... 1

Status
Not open for further replies.

song2siren

Programmer
Jun 4, 2003
103
GB
Hello

I've got a search page on an online store application and I just wanted some advice on the best way to allow a user to return to their search results from other pages in the application.

For example, I need to add a 'return to search results' button which navigates back to home.aspx from details.aspx.

I'm fairly sure I would need to do this by storing session state, but I'm really not clear on how to go about this. I can't just use a javascript back function unfortunately.

My search is performed like this:

Sub getResults(Source as Object, e as EventArgs)
cmdSql = New SqlCommand("Pubs_Search", myConnection)
cmdSql.CommandType = CommandType.StoredProcedure
parameterSearch = New SqlParameter("@Search", SqlDbType.NVarChar, 255)
parameterSearch.Value = frmSearchText.text
cmdSql.Parameters.Add(parameterSearch)
parameterList = New SqlParameter("@SearchList", SqlDbType.NVarChar, 255)
parameterList.Value = frmSearchList.selectedvalue
cmdSql.Parameters.Add(parameterList)
myConnection.Open()
strResults = cmdSql.ExecuteReader(CommandBehavior.CloseConnection)
dlResults.DataSource = strResults
BindDataList
End Sub

Sub BindDataList
dlResults.DataBind()
' Display a message if no results are found
If dlResults.Items.Count = 0 Then
ErrorMsg.Text = "No items matched your query."
else
ErrorMsg.Text = ""
End If
End Sub

Any help/suggestions would be very much appreciated.

Thanks
 
save the DataSet in which you store the results in a session variable then always use the session to retrieve the result set and display it

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Hi dazzled

Sorry to be stupid, I've not tried to do this before. Do I save the DataSet to a session variable in my getResults sub? Also not sure how to retrieve it from the button on the details.aspx page.

Thanks again
 
you should use a sqldataadapter to fill a datatable with the results you need. then save the datatable to the session like Session("resultSetDT") = myDataTable;

when you need to retrieve it, simply use DataTable myDataTable = (DataTable)Session("resultSetDt")

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
OK

I've used the sqlDataAdapter and added the session as follows:

Sub getResults(Source as Object, e as EventArgs)
myDataAdapter = New SqlDataAdapter("Pubs_Search", myConnection)
myDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure
myDataAdapter.SelectCommand.Parameters.Add(New SqlParameter("@Search", SqlDbType.NVarChar, 255))
myDataAdapter.SelectCommand.Parameters("@Search").Value = frmSearchText.text
myDataAdapter.SelectCommand.Parameters.Add(New SqlParameter("@SearchList", SqlDbType.NVarChar, 255))
myDataAdapter.SelectCommand.Parameters("@SearchList").Value = frmSearchList.selectedvalue
resultDS = New DataSet()
myDataAdapter.Fill(resultDS, "Results")
dlResults.DataSource = resultDS.Tables("Results").DefaultView
BindDataList
myDataAdapter.Dispose()
myConnection.Close
End Sub

Sub BindDataList
dlResults.DataBind()
' Display a message if no results are found
If dlResults.Items.Count = 0 Then
ErrorMsg.Text = "No items matched your query."
else
ErrorMsg.Text = ""
End If
Session( "resultDS" ) = resultDS.Tables("Results").DefaultView
End Sub

However, I'm not sure if I'm loading the session variable correctly and I also can't seem to call it back in and populate my DataList when returning to Home.aspx from Details.aspx.

Apologies if I'm missing something really obvious - thanks again.
 
i think it would be best to save the entire resultDS.Tables("Results") in a session variable like:
Session("resultDSDataTable") = resultDS.Tables("Results");

then you need to use
DataTable myDataTable = (DataTable)Session("resultDSDataTable")
to retrieve the datatable that can then be set as a datasource for your datagrid.

consider saving the datatable in the session in the getResults sub then in the BindData sub use
DataTable myDataTable = (DataTable)Session("resultDSDataTable")
dlResults.DataSource = myDataTable.DefaultView
dlResults.DataBind()
...
the rest of the logic

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Thanks again.

I think I'm getting there - I've saved the session variable in the getResults sub, but the BindDataList sub only runs when a search is performed by clicking on a button on my form.

I think I need to check when the page is loaded whether there is a session variable and, if so, bind the variable to the datalist. Not sure how/where to execute this.

Thanks again.
 
i think you should call the results page (when returning to it) using a param like searchresult.aspx?isReturned=true

on Page_Load always check to see if this variable exists. if it does, call the bind sub. if it's not, set the session variable for the datatable to null and first call the getResults sub then the bind sub

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Many thanks for this dazzled.

I've added the following to my page_load:

If not(Session("resultDSDataTable")is nothing) Then
dlResults.DataSource = Session("resultDSDataTable")
BindDataList
End If

This seems to work OK.
 
the problem is that maybe the user will want to navigate somewhere else in the site and then come back to the page with the results. if this is allowed (meaning the user has a link for the results page) he will se the results of his last search. otherwise this is not an issue

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top