C# pass List<List<int>> to a Web Service - c#

The thread is exactly what I need,
I have this:
WebService:
....
List<List<Int64>> whau = new List<List<Int64>>();
....
[WebMethod]
static setList(List<List<Int64>> list_web)
{
whau = list_web;
}
C#
public void func1 ()
{
List<List<Int64>> list = new List<List<Int64>>();
List<Int64> sublist = new List<Int64>();
sublist.Add(1);
sublist.Add(2);
list.Add(sublist);
service.setList(????);
}
But nothing works, I mean I've tried to send a List to the WebService, I used
sublist.ToArray()
and that works, but how to send the
List<List<>> var ?
Need really help !!
Edit :
I've already tried to do this :
service.setList(list);
and that works if the WebMethod is near the func1(), but of course the goal of WebService is not to be implemented in the same place that the Business Software...

Change your List<List<T>> to T[,] and see if it works.
Your code will look like
....
Int64[,] whau = new Int64[X,Y]; // I don't know the size of this.
....
[WebMethod]
static setList(Int64[,] list_web)
{
whau = list_web;
}
public void func1 ()
{
Int64[,] list = Int64[1, 2];
list[0, 1] = 1;
list[0, 2] = 2;
service.setList(list);
}
Remember that this is only an example. I don't know your actual code. This may cause compilation errors, but I think they will be easy to solve out.

Ok budies,
so what I ve done is simple finally,
I just send a list to the webservice,
and the webservice recieve that list and insert it to another list,
so is the WebService who creates the Nested List.
Thank you very much about your help you all !!
Regards !
FB

Related

WebUntisAPI returns "not Authenticated" after calling method in WPF project

To keep it short:
Following method calls a method from the WebUntisAPI (here we look for all rooms):
(this.sessionID has stored the session ID i requested in a authenticationmethod before)
public void getRooms()
{
client.AddHandler(new JsonDeserializer(), "application/json-rpc");
var request = new RestRequest(Method.POST);
request.AddHeader("Content-type", "application/jason");
request.AddJsonBody(new
{
id = this.sessionID,
method = "getRooms",
#params = new {},
jsonrpc = "2.0"
});
var response = this.client.Execute(request);
var deserialized = JsonConvert.DeserializeObject<GetRoomsResponse>(response.Content);
try
{
foreach (GetRoomsResult i in deserialized.res)
{
Console.WriteLine(i.name);
}
}catch(Exception e)
{
Console.WriteLine("Something wrong with deserialization!");
}
}
The Problem is, that the response of the is as followed:
{"jsonrpc":"2.0","id":"030BDA942554038735AE69C94D6B0491","error":{"message":"not authenticated","code":-8520}}
But the fun is not over yet!
If i copy the created json body and put him into postman, with the same key which is in the this.sessionID variable (of course also the same URL) it is working just fine.
If i generate the sessionID outside the programm and hardcode it to the variable it doesn't work as well.
Anybody already worked with the WebUntisAPI and knows why this doesn't work?
I think maybe it has nothing to do with WebUntis but with the way WPF works after compiling. I really don't know.
Thanks for any advice and help,
Fabian
edit:
WebUntisAPI documentation i got/found if you want to read it yourself

How to pass mapped array to PageMethods?

I've been trying to pass a data array to c# web method with using jquery. I have a table which has selectable rows. And my function must pass the id's of selected data. But i can't pass the object array with PageMethods.
Here is my jquery function;
function DeleteQuestions()
{
var table = $('#questTable').DataTable();
var data = (table.rows('.selected').data()).map(function (d) { return d.q_id; });
PageMethods.Delete(data);
}
When i debug it with firebug, veriable data looks like : Object["543","544","546"] as i wanted.
And here is my Web Method:
[WebMethod]
public static void Delete(List<string> questId)
{
DB_UserControl carrier = new DB_UserControl(); //break pointed here
}//and looks it doesn't come here
It doesn't work, and the error is : Cannot serialize object with cyclic reference within child properties. I've searced for error but i couldn't figured it out. So need some help. Thanks in advance.
Note:Error throws at script function's last line: PageMethods.Delete(data);
And i think it might be about mapped data causes some kind of loop behavior.
Problem solved with changing syntax. I use
var data = $.map(table.rows('.selected').data(), function (d) {
return d.q_id;
});
instead of given line. I don't know what caused the error but this code works fine and i get data in c#. Thank you all

Collection iteration in javascript using inline server tags

I have an asp.net user control which exposes a public IEnumerable object. This could be converted to a list or array if it helps to answer the question. What I would like to do is to loop through all of the server objects within a javascript function and do something with the contents of each item. I would like to achieve this using inline server tags if possible. Something like the below.
function iterateServerCollection()
{
foreach(<%=PublicCollection %>)
{
var somevalue = <%=PublicCollection.Current.SomeValue %>;
}
}
Is it possible to achieve this?
Managed to achieve what I wanted thanks to the comment from geedubb. Here's what the working javascript looks like.
var myCollection = <%= new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(myCollection) %>;
for(var i in myCollection)
{
var somevalue = myCollection[i].SomeValue;
}
I would do as others have suggested and serialize the object, you could even use an ajax call to grab the data from the start of that function
function iterateServerCollection()
{
//Ajax call to get data from server HttpHandler/WebAPI/Service etc.
for(var item in resultObject) {
//you can use item.WhateverPropertyIsOnObject
}
}

The following throws 'is a Method but treated like a type'

The most confusing error I have ever seen in ASP. I have done method calls like this before, and have no issue in other spots of my code.
First of all the class:
namespace LocApp.Helpers.Classes.LocationHelper
{
public class QueryHelper
{
private LocAppContext db = new LocAppContext();
public static IEnumerable<Service> getAllService()
{
using (var db = new LocAppContext())
{
var service = db.Locations.Include(s => s.LocationAssignment);
var serv = (from s in db.Services
where s.active == true
select s).ToList();
return serv;
}
}
}
}
Pretty easy to understand whats going on. So lets call the method:
IEnumerable<LocApp.Models.Service> Service = new LocApp.Helpers.Classes.LocationHelper.QueryHelper.getAllService(Model.id);
getAllServices(Model.id) is throwing the error "is a method but treated like a type" , um no its not be treated like a type....
whats going on?
Well it's exactly as the error message says. getAllService() is a method:
public static IEnumerable<Service> getAllService()
But you're trying to use it as if it were a type with a constructor:
Service = new LocApp.Helpers.Classes.LocationHelper.QueryHelper.getAllService(...)
The new part is the mistake here. You don't want to call a constructor, you just want to call a method. It's a static method, so you don't need an instance - you can just use:
Service = LocApp.Helpers.Classes.LocationHelper.QueryHelper.getAllService(...)
Note that if you have appropriate using directives, follow .NET naming conventions and take care about singular/plural names, your code will be easier to follow:
var services = QueryHelper.GetAllServices(...);
Do you not simply mean:
IEnumerable<LocApp.Models.Service> Service = LocApp.Helpers.Classes.LocationHelper.QueryHelper.getAllService();
Get rid of the new bit, essentially, and that method doesn't take any parameters either - I'd assume you'd run into that problem after you removed the new bit.
Your getAllService method doesn't take any arguments, so you should call it without. Also it is a static method so don't use the new keyword:
IEnumerable<LocApp.Models.Service> Service = LocApp.Helpers.Classes.LocationHelper.QueryHelper.getAllService();

WebMethod is not active/visible?

Ok I had one WebMethod in my web service which is working fine.
Then I wanted to add another one where I want to send whole object but when I trying to call this method from Windows Form its says method missing?
WebService code:
{
[WebMethod]
public int getNumber(int n)
{
return n * n * 100;
}
[WebMethod]
public string GetValues(string value)
{
return "OK";
}
}
Client code:
private void button1_Click_1(object sender, EventArgs e)
{
localhost.Service1 service2 = new localhost.Service1();
metadata detective = new metadata();
detective.CrimeID = txtCrimeID.Text;
detective.InvestigatorID = txtInvestID.Text;
detective.DataArtefactID = txtDataID.Text;
service2. **<== when I type here GetValues = "Menu.localhost.Service1 does not contain definition for GetValues"**
}
But if straight after "service2." i will start typing get, then method getNumber will be displayed as a possible choice. I dont uderstand Why one method is working fine but another one looks like not exist?
After modifying the webservice, the changes will not propogate to your winforms application until you update the reference to it. The available methods are stored in meta-data for the service when the reference is created.
Update your service reference.

Categories