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...