-
Call WebService from JS
UpdateMarks.asmx ( LOCATION ROOT FOLDER)
Code:
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data.SqlClient;
using System.Data;
/// <summary>
/// Summary description for UpdateMarks
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class UpdateMarks : System.Web.Services.WebService
{
public SqlConnection con = new SqlConnection("Data source = SONIA-PC;Initial Catalog=master;Integrated Security=true");
string query;
SqlCommand cmd;
DataSet ds = new DataSet();
SqlDataAdapter da;
[WebMethod]
public string GetMarks(string Roll)
{
string Marks;
cmd = new SqlCommand("Select Marks from student where Roll = '" + Roll + "'", con);
Marks = cmd.ExecuteScalar().ToString();
return Marks;
}
}
ASPX PAGE
Code:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title> </head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate >
<asp:DropDownList ID="cmbRoll" runat="server">
</asp:DropDownList>
<asp:TextBox ID="txtSubjectMarks" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
<script src =~/Common.js" type ="text/javascript" ></script>
<script language="javascript" type ="text/javascript" >
function UpdateTotalMarks()
{
alert("UpdateTotalMarks");
var cmbRoll_Index = document.getElementById('cmbRoll').selectedIndex;
alert(cmbRoll_Index);
var cmbRoll_Text = document.getElementById('cmbRoll').options[cmbRoll_Index].text;
alert(cmbRoll_Text);
var validValue = GetSynchronousJSONResponse('<%=Page.ResolveUrl("~/UpdateMarks.asmx/GetMarks") %>', '{"Roll":"' + cmbRoll_Text + '"}');
alert("a");
correct = eval('(' + validValue + ')');
if(isNaN(correct.d))
{
alert("0");
}
else
{
document.getElementById('<%= TextBox1.ClientID %>').value = ConvertToPrecision(correct.d);
}
}
</script>
</html>
SERVER SIDE CODE : -
Code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
txtSubjectMarks.Attributes.Add("onKeyUp", "UpdateTotalMarks();");
Common.JS (LOCATION - Root FOLDER)
// JScript File
Code:
function GetSynchronousJSONResponse(url, postData) {
var xmlhttp = null;
if (window.XMLHttpRequest)
xmlhttp = new XMLHttpRequest();
else if (window.ActiveXObject) {
if (new ActiveXObject("Microsoft.XMLHTTP"))
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
else
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
url = url + "?rnd=" + Math.random(); // to be ensure non-cached version
xmlhttp.open("POST", url, false);
xmlhttp.setRequestHeader("Content-Type", "application/json; charset=utf-8");
xmlhttp.send(postData);
var responseText = xmlhttp.responseText;
return responseText;
}
function ConvertToPrecision(value)
{
return Math.round (value *10000)/10000;
}
What i want to do?? I have two textboxes & One dropdownlist that contains suppose the roll nos. In one textbox I entered any number, as the number is entered, I want that marks of Roll (SELECTED FROM DROPDOWNLIST) are selected from the database + ADD the MArks entered in textbox.
ERROR - My webservice is not returning any value. I think so webservice is not called. I am not getting where the error?? PLEASE HELP ME OUT!!!