views:

65

answers:

2

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.)

A: 

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.

Kragen
As I said, it's when I try to access results that I get an error. So using code after the SqlDataSource2.Select statement such as: System.Data.DataRow dr = dv.Table.Rows[0];Throw the error. When debugging, I can't find where the retrieved results are actually stored, which is ultimately my question - how do I access these results? Am I doing it correctly but getting errors, or doing it incorrectly hence the errors?
VickyB
@VickyB - Have you tried using an SQL profiler to record the exact SQL statement executed to make sure that it is executing the SQL you expect it to with the parameters you expect it to?
Kragen
I ran the SQL Profiler, and it doesn't seem to run the procedure... none of the SP event classes show my procedure in their text data. Does this mean that my procedure isn't actually being run?
VickyB
@VickyB - I guess so. You could try executing some SQL statements in SQL Server Management Studio to make sure that the trace is correctly recording events, but it looks to be that way (it would explain why you aren't seeing any results! :-p)
Kragen
The SQL Profiler seems to be working, it shows my SP when I run it from the Management Studio... is my syntax for SqlDataSource.Select incorrect?
VickyB
+1  A: 

Answer provided on ASP.NET Forums:

http://forums.asp.net/p/1583276/3995150.aspx#3995150

VickyB