i have a webmethod that call it by jquery ajax .in web method i bind repeater data source to pagedatasourse object but when i run my program if i dont use of static key word webmethode before method name jquery ajax method dose not work properly and if use static keyword i have this error
Object reference not set to an instance of an object….System.NullReferenceException: Object reference not set to an instance of an object.
and pagedatasourse fall in exception.i confused.what is solution?
thank you very much
its my jquery function
$(function () {
var x = 0;
$('.c1').bind('click', function () {
counter = counter + 1;
$.ajax(
{
type: "POST",
url: "WebForm1.aspx/bringdata",
data: { counter: counter },
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
cache: false,
success: function (ret) {
alert("success");
},
error: function (x, e) {
alert("error ");
}
}
);
})
$('.c2').bind('click', function () {
x = x - 1;
})
})
and its code behind :
[WebMethod]
public static void bringdata(int counter){
SqlConnection con = new SqlConnection("data source=.;database=site;integrated security=true;");
int cnt;
string sSQL = "Select username ,average,weight,point,password ,kal, Rank() over(order by point desc) as 'ranking' from karbar order by point desc";
SqlCommand cmd = new SqlCommand(sSQL, con);
SqlDataAdapter adapt = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapt.Fill(ds);
cnt=ds.Tables[0].Rows.Count;
PagedDataSource pds = new PagedDataSource();
pds.AllowPaging=true;
pds.DataSource=ds.Tables[0].DefaultView;
pds.PageSize=5;
pds.CurrentPageIndex=counter;
int vcnt=cnt/pds.PageSize;
rptList.DataSource = pds;
rptList.DataBind();
}
Why you get exception?
My guess would be, because APS.NET disposes the DataSet (not seen here) after each request, while you are trying to page results from the previous request. If you make a DataSet static (it’s a big no-no in ASP.NET) then it is kept in the memory and is also shared between all the requests.
How to fix this?
You are using incorrect data access pattern. Datasets are really fat bad boys and practically should not be used with ASP.NET.
List<Record>and populate it withDataReader.