Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#WebService
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 4-Sep-12 20:08pm
Comments
Sergey Alexandrovich Kryukov at 5-Sep-12 3:46am
   
Are you sure you want a Web service? Will WCF service work for you? --SA

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
[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
<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...
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 sanket saxena 355
1 Abhinav S 303
2 Sergey Alexandrovich Kryukov 299
3 OriginalGriff 225
4 thatraja 220
0 Sergey Alexandrovich Kryukov 8,497
1 OriginalGriff 4,850
2 Peter Leow 3,839
3 Maciej Los 3,535
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 5 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid