Click here to Skip to main content
16,016,570 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 its normal because jquery ajax only work with static method and if use static keyword i have this error
Quote:
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
JavaScript
<blockquote class="FQ"><div class="FQA">Quote:</div>$(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 behiend
C#
<blockquote class="FQ"><div class="FQA">Quote:</div>
    [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();


   }
Posted
Updated 3-Aug-12 19:37pm
v2
Comments
Aditya Mangipudi 3-Aug-12 13:12pm    
i am guessing your query is not returning results. Try to check the adapt value and make sure its not null.

SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand(
queryString, connection);
adapter.Fill(dataset);
behrad110 4-Aug-12 1:36am    
query is ok and i test it without jqueryajax.my problem is static keyword.and i cant bind repeater in static method

1 solution

Object reference not set to an instance of an object

This error happens when you try to use a property or call a method of an object that is null. More details: here[^]

A simple use of Visual studio DEBUGGER can tell you the object because of which it is happening. Just look at the stack trace and put a debugger on that line. Check the objects of that line and see if any one is null and you are trying to use that objects property. Handle the same.
 
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