【发布时间】:2012-08-03 09:22:37
【问题描述】:
我有一个通过 jquery ajax 调用它的 webmethod。在 web 方法中我将转发器数据源绑定到 pagedatasourse 对象但是当我运行我的程序时,如果我在方法名称之前不使用静态关键字 webmethode jquery ajax 方法不能正常工作如果使用静态关键字我有这个错误
对象引用未设置为对象的实例....System.NullReferenceException: 对象引用未设置为对象的实例。
和 pagedatasourse 属于异常。我很困惑。什么是解决方案? 非常感谢你 它是我的 jquery 函数
$(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;
})
})
及其背后的代码:
[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();
}
【问题讨论】:
-
我认为问题出在您的网络方法中。你有整个例外吗?
-
hi.problem 是静态 keword.and 它的例外:对象引用未设置为对象的实例。说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。源错误:第 67 行:rptList.DataSource = pds;第 68 行:rptList.DataBind();第 69 行:
标签: c# jquery asp.net c#-4.0 c#-3.0