Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi,

I am an embedded software developer (for microcontrollers) with limited web development experience.

I am writing a simple webservice with a webform to take data received by a webmethod and display it on a web form (as part of a tech demo).

See below - I highly doubt the proposed solution is valid, but it is the easiest way of showing what I am trying to do.


C#
[WebMethod]
public string UploadData(string Password, string Serial, string ConnectionReason, string ResetReason)
{
     txtPassword.Text = Password;
     txtSerial.Text = Serial;

     return txtResponse.text
}


Similarly, I would like the HTTP response to the webmethod to return the value of some textboxes back to the Client.

What is the most straightforward way of implementing this?
Posted
Comments
Sergey Alexandrovich Kryukov 11-Nov-14 14:49pm    
First thing which catches my eye is the worst thing, trying to use strings for all data. Why not using numeric data, enumeration, and so on? Apparently, you should have smalls set of values: very few connection reasons, very few reset reasons. Also, is it between .NET and .NET, or should your service be provided for wider range of technologies?
Also, do you really use embedded service expose to all the Internet? If you want to work only on LAN (I would presume), you could use way more flexible, powerful and simple, general-case WCF, not necessarily hosted on a Web service...
—SA
vonny232 12-Nov-14 4:26am    
Hi, Yes the Reset and Connection reasons should be numeric, but I have only just implemented the webmethod so the code is incomplete.

I will be interfacing between a PIC running the Microchip TCP/IP stack and the server (which is only supposed to be a tech demo as I am not a web developer). We only produce the hardware, so our customer will end up writing the web service and server code.

I will be using HTTP Post to send the data.

1 solution

Simply you can accomplish this using jQuery Ajax very easily :-

Let say below is the web method i am having as asmx
C#
[WebMethod(EnableSession = true)]
        public string GetData(int id, string name)
        {
            string jsonData = string.Empty;
	    // var data = get data from database here;
            jsonData = Parser.EntityToJSon(data);
            return jsonData;
        }


Here below one is the jQuery ajax code for the ajax call to this web method for fetching data :-

JavaScript
$(function(){
	$.ajax({
        type: "POST",
        url: "../WebMethods/WebService.asmx/GetData",
        data: "{Id:" + {pass ID here} + ",Name:'" + {pass name value here} + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            var result = $.parseJSON(response.d);
		// here 'result' is the JSON formatted data you wanted to fetch
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            // error happened, handle here
        }
    });
});


Hope this will definitely help you to proceed further.
Please let me know if anything else stops you to accomplish this.
 
Share this answer
 
Comments
vonny232 12-Nov-14 4:35am    
Hi,
I cannot understand how this will help me.

I have a microcontroller with a TCP stack which is calling the webmethod (so the ajax script should not be calling the webmethod).

All I want is to copy some values to/from the webmethod to a webform.

i.e.
1) Microcontroller sends data to the webmethod.
2) Webmethod copies values TO textboxes in the webform.
3) webmethod copies values FROM textboxes in the webform and returns to the Microcontroller.
SRS(The Coder) 12-Nov-14 5:29am    
Sorry, I misunderstood the scenario on the issue you explained , but in this case you can go like this :-
i) Within an event handler let say 'Page_Load' event handler for the page call the WebMethod which will simply get the data from the microcontroller and return it as string. Now this string data we can set as text for the textbox in page load in code behind.

Microcontroller >> WebMethod >> Page Event to load data into textbox.

Because you can not access the server controls to set value within a static web method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900