Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to create a service which is called by a web server. I am a beginner in this part. So please give me some hints or direction about how should I start my work.
Thank you.
Posted
Comments
Sergey Alexandrovich Kryukov 5-Sep-12 3:46am    
Are you sure you want a Web service? Will WCF service work for you?
--SA

1 solution

Goto to add new Item from your Project
Add WebService.asmx
one file is added under App_Code folder WebService.cs
Goto That file
Here you have to write which method you want to create..Here i am giving you an example:
XML
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
[WebMethod]//added these else code doesn't work
public List<City> GetNews()
{
    List<City> list = new List<City>();
    string constr = "Data Source=localhost;Initial Catalog=DBname;User Id=sa;Password=lock";
    string query = "SELECT top 20 news_id,news_title FROM news order by news_id DESC";
    SqlDataAdapter da = new SqlDataAdapter(query, con);
    DataSet ds = new DataSet();
    da.Fill(ds);
    list = ds.Tables[0].AsEnumerable().Select(data => new City() { Id = (int)data["news_id"], title = (string)data["news_title"] }).ToList();
    return list;
}
public class City
{
    public int Id { get; set; }
    public string title { get; set; }
}


Now i have called these web service from another page like this way
XML
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $.ajax({
            type: "POST",
            url: "WebService.asmx/GetNews",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                var cars = response.d;
                $('#output4').empty();
                $.each(cars, function (index, car) {
                    var url = "Mobile.aspx?id=" + car.Id;
                    $('#output4').append('<li><a href="' + url + '">' + car.title + '</a></li>');
                });
                $('#output4').append('<li style="color:Red; font-size:small; text-align:right"><a href="AllMobiles.aspx">view more</a></li>');
            },
            failure: function (msg) {
                $('#output4').text(msg);
            }
        });
    });
</script>


If you still having problem feel free to ask...always here to support...
 
Share this answer
 

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