Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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


ASP.NET
<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:
HTML
<%@ 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.
Posted
v2

1 solution

One thing which I can figure out is wrong, is your Jquery syntax. Below is the corrected syntax for displaying value in span.

$("#spnProductID").text(prod.ProductID);
             $("#spnProductName").text(prod.Name);
             $("#spnProductNumber").text(prod.ProductNumber);
 
Share this answer
 
v2
Comments
El Dev 20-Aug-13 13:03pm    
Still not working I did how you have corrected, this how I did:
function OnGetProductDetailsByProductIDComplete(result) {
var prod = eval("(" + result + ")");
$("spnProductID").text = prod.ProductID;
$("spnProductName").text = prod.Name;
$("spnProductNumber").text = prod.ProductNumber;
}
El Dev 20-Aug-13 13:07pm    
I have a question Mahesh...
I have followed an example from codeproject, the way they are connecting to the db is different as the way I am usually connecting to db and it is the first time I saw that way.
This is the way they are connecting to db:
string connectionString = "server=localhost;uid=sa;pwd=M77;database=TestDB;";

Once I am connecting with string connectionString = "server=localhost;uid=sa;pwd=M77;database=TestDB;"; I am getting this alert error from browser:

The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the <servicedebug> configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework SDK documentation and inspect the server trace logs.

what is the problem?
Mahesh Bailwal 20-Aug-13 13:11pm    
You did not copy my mentioned code properly.

Wrong : $("spnProductID").text = prod.ProductID;

Correct : $("#spnProductID").text(prod.ProductID);

El Dev 20-Aug-13 13:22pm    
Thanks Mahesh it is working....

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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