开发者

Accessing the result set of a stored procedure, executed using SqlDataSource.Select

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开发者_JAVA百科.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.)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜