-3

i am developing a Spring .net Application.

I have a Login Page with fields - LoginID Password

On Login-There is a form to be filled by the candidate so after Login I check if the candidate has already filled form then the values are fetched from the database and shown in the form fields. Now When the Candidate hasn't filled the form the database null rows for it and hence it gives me the above error..please tell me how should I resolve this error.

I tried checking if the obj is null but it does not go to to the next line to check of the obj is NULL it immediately throws the above mentioned exception

IT GIVES ERROR IN LINE 6 OF THE PAGE_LOAD FUNCTION EXCEPTION:EmptyResultDataAccessException was caught Incorrect Resultsize Expected 1 Actual 0

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user1274646
  • 921
  • 6
  • 21
  • 46
  • 1
    You still haven't told us what you've done to try to fix the error. What exception gets thrown, what's the error message, what line throws the exception? – Jason Watkins Mar 22 '13 at 05:45
  • 1
    If these objects are part of Spring.Net, I'd look at the documentation to see if there are any examples of what to do when the query returns no rows. If there isn't (and I'm pretty sure that a framework like Spring.Net would be mature enough to handle that case), then I'd wrap the call in a try-catch block in your overridden method, rather than simply returning the result, as it sounds like something is going wrong in the DB call. – Tim Mar 22 '13 at 05:46
  • haven't u done proper validations? – Freelancer Mar 22 '13 at 05:53
  • A little research in the documentation yields the following: `QueryForObject - Execute a query mapping the result set to an object using a IRowMapper. Exception is thrown if the query does not return exactly one object.` Since you're returning nothing (0), you're getting the exception. – Tim Mar 22 '13 at 05:53
  • @JasonWatkins hi i hav written the exception name and Line No. – user1274646 Mar 22 '13 at 05:54
  • Ok. Now remove the try/catch from `Page_Load` so that you can see what line in `CandidateSession` is throwing the exception. – Jason Watkins Mar 22 '13 at 05:56
  • I've never worked in Spring.NET, but looking at the documentation I think you'd be better of to use `QueryWithRowMapper(CommandType.StoredProcedure, "spGetCandidateSession", candidateMapper, parameters);`, as this deals with multiple objects (which I think could include null). Per the documentation, `QueryForObject` is designed for one object, no more, no less. – Tim Mar 22 '13 at 06:01

1 Answers1

1

As I said above, I've never worked with Spring.NET, but taking a look at the documentation I believe you may be using the wrong method of the AdoTemplate class. QueryForObject, per the documentation, will `Execute a query mapping the result set to an object using a IRowMapper. Exception is thrown if the query does not return exactly one object.'

In your example, if the user is not registered, they won't be in the database, so 0 results will be returned. Hence the error Incorrect Resultsize Expected 1 Actual 0.

Try using the QueryWithRowMapper method, as it is (again, per the documentation) one of the methods for Mapping result sets to objects:

return (CandidateSession)template.QueryWithRowMapper(CommandType.StoredProcedure, "spGetCandidateSession", candidateMapper, parameters);

If the cast to CandidateSession does not fail, I would expect a null object to be returned.

Again, I've never worked in Spring.NET, so this is based on a fairly cursory reading of the documentation. Hopefully it will at least get you going down the right path.

Tim
  • 28,212
  • 8
  • 63
  • 76