Webmethod call from jquery ajax and problemm with static object

I have a web method that calls its jquery ajax. In the web method, I bind the data source repeater to the pagedatasourse object, but when I run my program, if I do not use the static webmethode keyword before the jquery ajax method name, the method does not work correctly and if I use the static keyword, I have this error

The reference to the object is not set to the instance of the object .... System.NullReferenceException: the reference to the object is not set to the instance of the object.

and pagedatasourse fall into exception.i confused.What is the solution? Thanks a lot for its 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:

  [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(); 

}

+4
source share
3 answers

Why are you getting an exception?

My guess would be because APS.NET provides a DataSet (not seen here) after each query, while you are trying to output the results from a previous query. If you put a static DataSet (it is a big no-no in ASP.NET), then it is stored in memory and is also shared between all requests.

How to fix it?

You are using the wrong data access pattern. Datasets are really fat bad boys and should hardly be used with ASP.NET.

  • Get rid of the DataSet completely. Do not use static objects, when you get some traffic for your application, you will see real strange errors.
  • Use something lighter, such as List<Record> , and populate it with DataReader .
  • Use database swap, not memory swap. Never request a database server to transfer all data, otherwise your application will decrease (when you get some traffic and the database becomes larger).
  • Consider using ORM (e.g. NHibernate or EF), this will do a lot of magic for you.
+3
source

The first of all web methods should be static, because we call them directly without creating any objects. And secondly, we cannot access server-side controls directly in the web method.

Here you can get a client-side data source and associate it with a client-side repeater.

 success: function (ret) { // here get the datasource as "ret". // bind the repeater with the data source manually. } 

OR

Put your controls inside the update panel, then you will not need any client database, and you will not need any ajax call in jQuery.

0
source

The rptList element is null; create a new rptList before trying to use it.

0
source

All Articles