访问使用 SqlDataSource.Select 执行的存储过程的结果集

发布于 2024-09-11 03:14:58 字数 1240 浏览 2 评论 0原文

我正在 ASP.NET 中编写一个网页,该网页显示未列为某项活动参与者的学生列表,单击学生姓名后会显示其详细信息的简要摘要,以便用户可以确保他们选择了正确的人。

我的代码当前正确获取了它们的 ID,将其作为参数添加到我的存储过程中并执行该过程;

protected void LinkButton_OnClick(object sender, EventArgs e)
{
    LinkButton l = (LinkButton)sender;
    HiddenField hfv = (HiddenField)l.Parent.FindControl("hfAdmissionNumber");
    SqlDataSource2.SelectParameters.Clear();
    SqlDataSource2.DataSourceMode = SqlDataSourceMode.DataReader;
    SqlDataSource2.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
    Parameter hfcParam = new Parameter();
    hfcParam.Type = TypeCode.Int32;
    hfcParam.DefaultValue = hfv.Value;
    hfcParam.Name = "@AdmissionNumber";
    hfcParam.Direction = System.Data.ParameterDirection.Input;
    SqlDataSource2.SelectParameters.Insert(0, hfcParam);
    System.Data.DataView dv = (System.Data.DataView)SqlDataSource2.Select(DataSourceSelectArguments.Empty);
}

但是,当我尝试访问结果时,出现以下错误:

System.NullReferenceException: Object reference not set to an instance of an object.

在调试时,似乎没有返回任何结果...但是当仅使用相同数据在 SQL Server 中运行存储过程时,它确实返回单行,如预期。

如何访问此结果以便将其绑定到我的字段?

(我正在 Visual Studio 2008 中使用 ASP.NET 3.5 和 SQL Server 2008。)

I'm programming a webpage in ASP.NET which displays a list of students not listed as participants in something, and upon clicking a student name shows you a brief summary of their details, so the user can ensure they're selecting the right person.

My code currently correctly obtains their ID, adds it as a parameter to my stored procedure and executes the procedure;

protected void LinkButton_OnClick(object sender, EventArgs e)
{
    LinkButton l = (LinkButton)sender;
    HiddenField hfv = (HiddenField)l.Parent.FindControl("hfAdmissionNumber");
    SqlDataSource2.SelectParameters.Clear();
    SqlDataSource2.DataSourceMode = SqlDataSourceMode.DataReader;
    SqlDataSource2.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
    Parameter hfcParam = new Parameter();
    hfcParam.Type = TypeCode.Int32;
    hfcParam.DefaultValue = hfv.Value;
    hfcParam.Name = "@AdmissionNumber";
    hfcParam.Direction = System.Data.ParameterDirection.Input;
    SqlDataSource2.SelectParameters.Insert(0, hfcParam);
    System.Data.DataView dv = (System.Data.DataView)SqlDataSource2.Select(DataSourceSelectArguments.Empty);
}

However, when I try to access the results I get the following error:

System.NullReferenceException: Object reference not set to an instance of an object.

Upon debugging, there don't seem to be any results returned... but when running just the stored procedure in SQL Server with the same data, it does return a single row, as expected.

How do I access this result so I can bind it to my fields?

(I am working in ASP.NET 3.5 in Visual Studio 2008, with SQL Server 2008.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风透绣罗衣 2024-09-18 03:14:58

异常应该告诉您有关错误的更多信息,特别是发生错误的行。您应该专门查看这一行,尝试确定到底什么是 null,并找出如何防范它。

例如,在上面的代码中,我可以看到您有以下内容:(为了清楚起见,删除了中间代码)

HiddenField hfv = (HiddenField)l.Parent.FindControl("hfAdmissionNumber");
hfcParam.DefaultValue = hfv.Value;

在这种情况下,我可以看到 FindControl 方法可以返回 null,在这种情况下,当尝试执行 hfv.Value 时,您将收到 NullReferenceException,因为 hfv 为 null。

如果您能弄清楚到底什么是 null,那么应该可以更好地指示问题所在。

The exception should tell you some more information about the error, specifically the line on which the error occurred. You should look specifically on this line to try and determine what exactly is null, and figure out how to protect against it.

For example, in the above code I can see that you have the following: (intermediate code removed for clarity)

HiddenField hfv = (HiddenField)l.Parent.FindControl("hfAdmissionNumber");
hfcParam.DefaultValue = hfv.Value;

In this case, I can see that potentially the FindControl method could return null, in which case you would get a NullReferenceException when attempting to do hfv.Value as hfv is null.

If you can figure out what exactly is null, that should give you a better indication of what the problem is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文