Sunday, March 11, 2012

Ajax slideshow error: The server method GetSlides failed with the following error: System.

Hi,

Please note the the URL for this GetSlides method isn't the one you see in the address bar of your browser, so it's likely that querystring ProjID doesn't exist. Actually, a different thread is used to server this request.

I suggest not using querystring but contextKey to pass the projID into it.

For instance:

<ajaxToolkit:SlideShowExtender ID="slideshowextend1" runat="server"
TargetControlID="Image1" BehaviorID="ss1" ContextKey="projID to be passed // it can also be assigned in code behind too" ...... />

PublicSharedFunction GetSlides(byref contextKey as string)As AjaxControlToolkit.Slide

Dim querystrAsString =contextKey ..................

Hope this helps.


Hello Raymond,

Thank you very much for your reply. However, I am not really sure how to implement your suggestion as I am not that familiar with contextkey. Would you mind to elaborate your suggestion in more detail or have you come across any example on using similar method that I intend to use?

Many thanks.


You can set the contextKey in Page_Load method.

function Page_Load()

slideshowextend1.ContextKey = Request.QueryString["projid"]

Then get this value via parameter contextkey in GetSlides method

PublicSharedFunction GetSlides(byrefcontextKeyas string)As AjaxControlToolkit.Slide

Dim querystrAsString =contextKey ..................


Thanks for the immediate reply Raymond. Really appreciated.

I tried to use the contextkey as demonstrated, but I think that I might have overlooked something because now I am seeing this new error:

The server method "GetSlides" failed with the following error: System.InValidOperationException--cannot convert object of type 'system.string' to 'system.string&'

My codes are as follows:

<%@. Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Copy of Project.aspx.vb"Inherits="Project" title="Untitled Page" %><asp:Content ID="Content1" ContentPlaceHolderID="mainContentHolder" Runat="Server"><form id="form1"> <div align="center"> <asp:Image ID="Image1" runat="server" ImageUrl="" Width="300px" /> <br /> <br /> <asp:Button ID="prevButton" runat="server" Text="Previous" /> <asp:Button ID="playButton" runat="server" Text="Play" /> <asp:Button ID="nextButton" runat="server" Text="Next" /> <ajaxToolkit:SlideShowExtender ID="SlideShowExtender1" TargetControlID="Image1" runat="server" AutoPlay="true" NextButtonID="nextButton" PlayButtonID="playButton" PlayButtonText="Play" PreviousButtonID="prevButton" StopButtonText="Stop" ImageDescriptionLabelID="Label1"Loop="true" SlideShowServiceMethod="GetSlides"> </ajaxToolkit:SlideShowExtender> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </div> </form></asp:Content>Imports SystemImports System.DataImports System.Data.SqlImports System.Data.SqlClientImports System.ConfigurationImports System.CollectionsImports System.WebImports System.Web.SecurityImports System.Web.UIImports System.Web.UI.WebControlsImports System.Web.UI.WebControls.WebPartsImports System.Web.UI.HtmlControlsImports System.Web.HttpContextPartialClass ProjectInherits System.Web.UI.Page <System.Web.Services.WebMethod()> _ <System.Web.Script.Services.ScriptMethod()> _Public Shared Function GetSlides(ByRef contextKeyAs String)As AjaxControlToolkit.Slide()Dim cnnAs New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)Dim querystrAs String = contextKeyDim cmdAs New SqlCommand("SELECT * from photoTable Where([ProjID] = " & querystr &")", cnn)Dim drAs SqlDataReaderDim sqlDataAdapterAs SqlDataAdapterDim dataSetAs DataSet'instantiate SqlAdapter and DataSet sqlDataAdapter =New SqlDataAdapter(cmd) dataSet =New DataSet()'populate the DataSet sqlDataAdapter.Fill(dataSet,"Photo")Dim countAs Integer = dataSet.Tables("Photo").Rows.Count() - 1Dim scriptstring(count)As AjaxControlToolkit.SlideTry cnn.Open() dr = cmd.ExecuteReader()Dim iAs Integer = 0Do While dr.Read() scriptstring(i) =New AjaxControlToolkit.Slide("ProjPhotos/" & dr("PjtPhotoLarge"),"","") i = i + 1Loop Catch eAs ExceptionFinally If Not cmdIs Nothing Then cmd.Dispose()End If If Not cnnIs Nothing Then cnn.Close() cnn.Dispose()End If End Try Return scriptstringEnd Function Protected Sub Page_Load(ByVal senderAs Object,ByVal eAs System.EventArgs)Handles Me.Load SlideShowExtender1.ContextKey = Request.QueryString("ProjID")End SubEnd Class

Hope that you can shed some light into my problem. Thank you very much.


Sorry, I made a mistake in my previous post. The parameter should be passed by value, not passed by reference. 
Public Shared Function GetSlides(ByValcontextKeyAs String)As AjaxControlToolkit.Slide()


Thanks Raymond. It works!


I am getting same error with IndexOutOfRange exception. Here is my code:

Imports System.Web

ImportsSystem.Web.Services

ImportsSystem.Web.Services.Protocols

ImportsSystem.Web.Script.Services

Imports System.IO

<WebService(Namespace:="http://tempuri.org/")> _

<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)>_

<System.Web.Script.Services.ScriptService()>_

PublicClass GetSlides_WebService

InheritsSystem.Web.Services.WebService

<WebMethod()> _

<ScriptMethod()> _

PublicFunction GetSlides(ByValcontextKeyAsString)As AjaxControlToolkit.Slide()

DimstrFileNames()AsString= Directory.GetFiles(Server.MapPath("~\General\Images\home\images\"))

Dimphotos()As AjaxControlToolkit.Slide =New AjaxControlToolkit.Slide(strFileNames.Length - 1){}

IfstrFileNames.Length > 0Then

ForiAsInteger =0To strFileNames.Length - 1

DimstrFile()AsString= strFileNames(i).Split("\"c)

photos(i) =New AjaxControlToolkit.Slide("..\General\Images\home\images\"& strFile(strFileNames.Length - 1),"","")

Next

EndIf

Returnphotos

EndFunction

EndClass

It works in DEV but not in PRD.


No comments:

Post a Comment