Expected 1 Actual 0 [closed] - c#

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
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

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.

Related

Meaning of -1 in Programming [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I hate to ask this question on here, but I have searched both SO and Google to no success. I have seen in many places statements such as while(var != -1) and other statements, often loops, containing some sort of reference to -1. Is there a certain meaning to the use of -1, or is it just used as giving an integer representation of a boolean, or something like that? I have mainly seen this in C# programming if that is any help.
in C# -1 is just negative one. They're comparing a number against a number, seeing if it is indeed equal to negative one.
It's not uncommon to have an integer field that should only have positive values (for example, when representing an index in a list) and in such cases -1 is sometimes used to represent "not a valid value", for example, there is no item, and hence no index. They use -1 because an int is not nullable; they cannot assign null.
In theory this is probably a bad practice; it's using a "magic value" to mean something more than it really should. Ideally if "there is not valid" is a valid thing for the variable to represent it should be a nullable integer (int? or Nullable<int>) but this is an old convention (carried over from other languages without a feature for nullable ints) so it's hard to eliminate entirely.
Nothing special about it. It's just that in most frameworks and libraries, functions or methods that return an index of an element in a collection will return -1 when whatever you're looking for isn't in the collection.
For example, the index of the character b in the string foo would be -1 in JavaScript, .NET and, as far as I remember, Java as well.
So many devs have burned a rom in their minds saying that -1 is the index for not found items. Now you know why.
If you know that an int should always contain positive value (for instance an item count or an index in a list, -1 can be a kind of "reserved value", so you would for instance assign the count to -1 and as long as it's -1, you know no real value has been put in there, a bit like a "null"
other than that I don't think there's any special meaning to -1

can storing data in a database sometimes lead to corrupted data? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have a field that's stored in the database as a string. It's actually a comma-separated string of numbers that I convert to a list of longs. The lines of code that do the conversion look somewhat like this:
TheListOfLongs = (from string s in StringFromDB.Split(',')
select Convert.ToInt64(s)).ToList<long>();
The code that creates the database storage string looks like this:
return String.Join(",", TheListOfLongs.Select(x=> x.ToString()).ToArray());
This works fine but as you can see, if for some reason there's a problem with the string, the code in the first line of code breaks on Convert.ToInt64(s).
Now I know I can wrap all this around a try statement but my question is this: can storing and retrieving a string in the database corrupt the string (in which case I definitely need the try statement) or is this a one a trillion odd type of event?
I wouldn't worry about corrupt data per se. However, you definitely need to handle the more general case where you can't parse what should be numeric data.
Even in the most controlled situations it is good programming practice to provide conditions for when you can't process data as you're expecting to be able to. What that means to your application is something you'll need to decide. Wrapping the statement with a try..catch will prevent your application from choking, but may not be appropriate if the parsed list is critical later on.
Selecting from the DB shouldn't corrupt your string.
If the connection is dropped mid transfer or something like that then an exception is thrown.

Should methods return nil or empty array if no data was found/calculated? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
This is generic question and it's interesting what approach do you use guys and what are pros and cons? Sometimes methods return lists (for example, NSMutableArray or NSArray in obj-c) and my current approach is to return nil if there's nothing to return. It makes sense to return nil if it's just object of some native or custom class, however should we return alloc'ed/init'ed empty array if nothing was found?
The question is primarily targeted for objective-c but is also related to other languages. Objective-c has nice feature to call methods on nil, however this is not the case for all other languages. So it's also interesting to hear arguments from other devs (java, c# and PHP devs).
UPDATE It's strange why some are voting on closing the question. Design pattern related questions also could be described as personal opinion questions, however architecture & code quality questions are more important thing than just syntax questions.
There is not a single answer I believe.
For myself I decided to return the result of expected type if possible and NULL otherwise.
Say, in my homebrewed DAL the getAll method returns an empty array (list) - so it can be passed to PHP's foreach operator without raising an error.
On the other hand, the getRow method, which is supposed to return an associative array, returns NULL in case of none data found, as empty array would be as equally useless.
Returning a null and an empty container have two different meanings in my opinion. An empty container would mean, no result matched your query but it was successful, while a null result would mean something went wrong.
In the first case if you have written code somewhere to say:
print "You have found" + GetResults.Size() + " results"
You will be able to print that zero results matched the query. While if you decide on using null, you will have to add explicit checks.
I have also had some troubles with serializing result to JSON as a response to a web server request, later on I regretted that I chose to return null instead of empty array.
Return nil or whatever is clearly a non-thing. Don't do more work than needs to be done.
Returning an empty array may cause many useless pointers and structures to be allocated, when all you really need is to say, "I have nothing for you."
In Cocoa,
If you return a nil to some array you need to check afterwards, that you are not trying to access the array Index.
And it is good to return nil instead of an array without any object.

Returning type Java and C# [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Java and C# return type of methods are by reference or value? It's really confusing for me, need some explanation.
Thank you all.
In Java everything is returned by value. That includes references and here's where the confusion is!
If I have:
Trade t = new Trade();
then t is a reference (we'd say it is-a Trade, but that refers to the type. t really is a reference). When I return that from a method, I'm returning the reference, by value. The reference still points to that original object.
Hence if I return that t from a method and then invoke a further method on it, it invokes the method on the Trade that it originally pointed to.
C# can return results by either value or reference - it depends how you define the method.
Java can only ever return by value (or strictly speaking, return reference by value.)
As this little Memory Slogan goes in HeadFirst Book..
Roses are Red,
This poem is Choppy,
Passing By Value is
Passing By Copy.
In Java its always Value that is passed or returned.
Where as in C# it can return either by Reference or by Copy.

how to handle all sql exceptions throughout the page [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
"I DONOT WANT TO use the default error page tachnique, because i donot want the webpage to redirect!"
yes there is the try and catch
yes there are way to add exception handling mathods overwrite for controls
but what i need is,
it may just be a simple sql command,it may be a control such as formview, it may be a control such as datagrid, whatever it may be, when an illegal entry is done into the table of the database,
"THE BIG ERROR PAGE SHOULD NOT COME!!"
instead
a label at the top of the same page (where the illegal operation is performed) should display the error like
"error caused by "this control" and the error message is "null is not allowed in this field blah blah"
thank you
note:- am i asking for the complete code! well yes please guide me thanks
I think the bottom line is that you need to validate your data before you try to put it in your database.
Each control contains its own validation hooks. Check the documentation for the controls you are using on MSDN in order to find out how to properly validate the data contained in them. In your validation procedure you can then determine the controls containing the bad data and update your label.
The default error page is not an error handling technique as such. It is a failsafe for when your application fails. Your updates to the database are throwing boneheaded exceptions - exceptions which your code should never cause to happen - you shouldn't even handle them - they represent bugs in your code which need to be fixed.
Simply grabbing plain user input without validating, trying to get it into your database, and then trying to detect problems is a recipe for disaster. I hope that you're at least wrapping your database commands in transactions if you're doing this, otherwise you could end up with who knows what in your database.
Use a try and catch construct around your SQL queries: if the code in the catch block is hit, then call a method which makes your error label visible, and sets some error text relevant to the part of the code that failed.
This is the cleanest and most widely-accepted way of dealing with this; although you should have separation and encapsulation too. The user interface (ASP.NET pagebehind code, for example) shouldn't usually call straight through to a database; it would call business logic methods or Data Access Layer methods which would update the database. You'd still want to catch exceptions that are thrown from these layers, though. The 'best' you can do is to create a method that manages the state of your 'error' label.
Hope that helps.

Categories