Hi I am trying to retrieve data from db using a webservice but it is not working
I got 3 files:ProductService.cs,ProductServiceClient.aspx,ProductService.svc
Here is the code I am using :
ProductService.cs file:
public class ProductService
{
[OperationContract]
public string GetProductDetailsByProductID(int productID)
{
Product prod = new Product();
string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["dbConnectionString"].ToString();
using (SqlConnection connection = new SqlConnection(connectionString))
{
string sql = "Select Name, ProductNumber from dbo.Product Where ProductID = " + productID.ToString();
connection.Open();
SqlCommand command = new SqlCommand(sql, connection);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
prod.Name = reader["Name"].ToString();
prod.ProductNumber = reader["ProductNumber"].ToString();
prod.ProductID = productID;
}
}
MemoryStream stream = new MemoryStream();
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Product));
serializer.WriteObject(stream, prod);
stream.Position = 0;
StreamReader streamReader = new StreamReader(stream);
return streamReader.ReadToEnd();
}
}
[DataContract]
public class Product
{
[DataMember]
public int ProductID;
[DataMember]
public string Name;
[DataMember]
public string ProductNumber;
}
And this how I am calling or refernce the webservice
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
function pageLoad() {
}
function OnGetProductDetailsByProductID() {
ProductService.GetProductDetailsByProductID($get("txtProductID").value, OnGetProductDetailsByProductIDComplete, OnError);
}
function OnGetProductDetailsByProductIDComplete(result) {
var prod = eval("(" + result + ")");
$("spnProductID").innerText = prod.ProductID;
$("spnProductName").innerText = prod.Name;
$("spnProductNumber").innerText = prod.ProductNumber;
}
function OnError(errorMessage) {
alert(errorMessage.get_message());
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
Enter Product ID: <input type="text" id="txtProductID" name="txtProductID" />
<input type="button" value="Get Product Details" id="btnInvokeWebService" onclick="OnGetProductDetailsByProductID()" />
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/ProductService.svc" />
</Services>
</asp:ScriptManager>
<br /><br />
Product ID : <span id="spnProductID"></span> <br /><br />
Name :<span id="spnProductName"></span> <br /><br />
Product Number :<span id="spnProductNumber"></span> <br /><br />
</div>
</form>
and the last file is
ProductService.svc:
<%@ ServiceHost Language="C#" Debug="true" Service="ProductService" CodeBehind="~/App_Code/ProductService.cs" %>
I dont know why the data are not displayed.
Please can someone help by telling me why?
by the way I can see the data in the response in console panel of firebug but are not displayed in the browser.