Hello, I'm trying to use AutoCompleteExtender and I'm stuck with return type of my method in web service:
This is my web method ... I need the return value NabidnuteCPN displayed by AutoCompleteExtender ... when I try this method for example as datasource for GridView it works fine ... I need the same result for my AutoCompleteExtender but I don't know how to
make return value to be string or something suitable for my AutoCompleteExtender
[WebMethod]
public IQueryable NabidniCPN(string prefixText)
{
LINQDataContext db = new LINQDataContext();
var vysledek = db.spAPWeb_VyhledejDleCPN(prefixText);
var nabidnuteCPN = from n in vysledek
select new
{
n.cisloZbozi
};
return nabidnuteCPN.AsQueryable();
}
Hi, thank you for your reply ... I've tried it with an array:
[WebMethod]
public Array NabidniCPN(string prefixText)
{
LINQDataContext db = new LINQDataContext();
var vysledek = db.spAPWeb_VyhledejDleCPN(prefixText);
var nabidnuteCPN = from n in vysledek
select new
{
n.cisloZbozi
};
return nabidnuteCPN.ToArray();
}
I'm getting this error: You must implement a default accessor on System.Array because it inherits from ICollection.
...the field cisloZbozi is a string type in database
[WebMethod]
public string[] NabidniCPN(string prefixText)
{
LINQDataContext db = new LINQDataContext();
var vysledek = db.spAPWeb_VyhledejDleCPN(prefixText);
var nabidnuteCPN = from n in vysledek
select new
{
n.cisloZbozi
};
return nabidnuteCPN.ToArray();
}
Like this I'm getting error: Cannot implicitly convert type 'AnonymousType#1[] to 'string[]'
try to add a debug point at the toarray function call and then see exactly what type is being returned, by right you should only return array of primitive types
Morignus
Member
86 Points
45 Posts
Cannot serialize interface System.Linq.IQueryable.
Feb 26, 2012 08:51 PM|LINK
Hello, I'm trying to use AutoCompleteExtender and I'm stuck with return type of my method in web service:
This is my web method ... I need the return value NabidnuteCPN displayed by AutoCompleteExtender ... when I try this method for example as datasource for GridView it works fine ... I need the same result for my AutoCompleteExtender but I don't know how to make return value to be string or something suitable for my AutoCompleteExtender
[WebMethod]
public IQueryable NabidniCPN(string prefixText) { LINQDataContext db = new LINQDataContext(); var vysledek = db.spAPWeb_VyhledejDleCPN(prefixText); var nabidnuteCPN = from n in vysledek select new { n.cisloZbozi }; return nabidnuteCPN.AsQueryable(); }Thank in advance
kwanann
Contributor
3816 Points
750 Posts
MVP
Re: Cannot serialize interface System.Linq.IQueryable.
Feb 27, 2012 05:31 AM|LINK
ya.. IQueryable is not serializable, what you can do is to return it as an array. do remember to change the function return type as well
do note that the field cisloZbozi must be a primitive type (i.e no binary, filestream, geolo types)
View my blog @ http://jefferytay.wordpress.com
Morignus
Member
86 Points
45 Posts
Re: Cannot serialize interface System.Linq.IQueryable.
Feb 27, 2012 05:58 AM|LINK
Hi, thank you for your reply ... I've tried it with an array:
[WebMethod] public Array NabidniCPN(string prefixText) { LINQDataContext db = new LINQDataContext(); var vysledek = db.spAPWeb_VyhledejDleCPN(prefixText); var nabidnuteCPN = from n in vysledek select new { n.cisloZbozi }; return nabidnuteCPN.ToArray(); }I'm getting this error: You must implement a default accessor on System.Array because it inherits from ICollection.
...the field cisloZbozi is a string type in database
kwanann
Contributor
3816 Points
750 Posts
MVP
Re: Cannot serialize interface System.Linq.IQueryable.
Feb 27, 2012 06:02 AM|LINK
since this is a string type, it means that the to array function returns a string array i.e
public string[] NabidniCPN(string prefixText)
View my blog @ http://jefferytay.wordpress.com
Morignus
Member
86 Points
45 Posts
Re: Cannot serialize interface System.Linq.IQueryable.
Feb 27, 2012 06:14 AM|LINK
Oh, ok. And how should look like return type ?
[WebMethod] public string[] NabidniCPN(string prefixText) { LINQDataContext db = new LINQDataContext(); var vysledek = db.spAPWeb_VyhledejDleCPN(prefixText); var nabidnuteCPN = from n in vysledek select new { n.cisloZbozi }; return nabidnuteCPN.ToArray(); }Like this I'm getting error: Cannot implicitly convert type 'AnonymousType#1[] to 'string[]'
kwanann
Contributor
3816 Points
750 Posts
MVP
Re: Cannot serialize interface System.Linq.IQueryable.
Feb 27, 2012 06:35 AM|LINK
are you sure cislozbozi is a string type?
try to add a debug point at the toarray function call and then see exactly what type is being returned, by right you should only return array of primitive types
View my blog @ http://jefferytay.wordpress.com
Morignus
Member
86 Points
45 Posts
Re: Cannot serialize interface System.Linq.IQueryable.
Feb 28, 2012 08:07 AM|LINK
cislozbozi is varchar in database, so it returns like char I think.
Morignus
Member
86 Points
45 Posts
Re: Cannot serialize interface System.Linq.IQueryable.
Feb 28, 2012 11:00 AM|LINK
I have fixed it.
[WebMethod] public string[] NabidniCPN(string prefixText) { LINQDataContext db = new LINQDataContext(); return db.spAPWeb_VyhledejDleCPN(prefixText).Select(n => n.cisloZbozi).ToArray(); }This works great and it is much cleaner code I think.
kwanann
Contributor
3816 Points
750 Posts
MVP
Re: Cannot serialize interface System.Linq.IQueryable.
Feb 29, 2012 06:29 AM|LINK
that will totally screw up your system!
[WebMethod] public string[] NabidniCPN(string prefixText) { using(LINQDataContext db = new LINQDataContext()) { return db.spAPWeb_VyhledejDleCPN(prefixText).Select(n => n.cisloZbozi).ToArray(); } }View my blog @ http://jefferytay.wordpress.com
Morignus
Member
86 Points
45 Posts
Re: Cannot serialize interface System.Linq.IQueryable.
Feb 29, 2012 01:09 PM|LINK
Hi, only difference from your's is the using, could you explain it to me?
Thank you.