Difficulty: Easy
Correct Answer: MessageBox.Show("Total is: " + cmd.Parameters["@ItemTotal"].Value.ToString());
Explanation:
Introduction / Context:
ADO.NET allows you to call stored procedures that return values through output parameters. When you execute the command with ExecuteNonQuery, the parameter collection is updated with any output or input/output values. This question checks whether you know how to correctly retrieve and display the value of an output parameter from a SqlCommand.Parameters collection in C#.
Given Data / Assumptions:
Concept / Approach:
Each SqlParameter in the Parameters collection exposes a Value property that contains the actual value after command execution. To read an output parameter, you must reference it by the same name you used when adding it (for example, "@ItemTotal") and then read its Value property. Because Value is of type object, you typically call ToString() on Value to display it. Calling ToString() directly on the SqlParameter object itself will return a type name, not the numeric value you want.
Step-by-Step Solution:
1. Configure the SqlCommand to call the stored procedure sp_GetDailyXYZSales.
2. Add the @ItemTotal parameter with its type set to SqlDbType.Int and Direction set to Output.
3. Open the SqlConnection and call cmd.ExecuteNonQuery() to run the stored procedure.
4. After execution, access cmd.Parameters["@ItemTotal"].Value to get the integer total returned by the procedure.
5. Convert the Value to a string, for example by calling .Value.ToString(), and concatenate it into a user-friendly message for a MessageBox.
Verification / Alternative check:
You can verify that the correct parameter is being read by temporarily logging the value to the console or debugging. If you intentionally change the stored procedure output and rerun the code, the displayed total should match the new result. If you mistakenly read a parameter by the wrong name or call ToString on the SqlParameter itself, the message box will not show the expected numeric total.
Why Other Options Are Wrong:
Option A and B incorrectly refer to "@Output", which is not the parameter name you created; the correct name is "@ItemTotal". Option B and D call ToString() on the SqlParameter object instead of on its Value property, which will typically return a type or name description instead of the actual integer value. Only Option C uses the correct parameter name and correctly reads the Value property, then converts it to a string.
Common Pitfalls:
A frequent mistake is mismatching parameter names between the stored procedure and the C# code, which can cause output values to appear empty or not update at all. Another pitfall is forgetting to set the Direction to Output, causing the Value to remain null. Developers also sometimes try to use ExecuteScalar for stored procedures that return values only through output parameters, which is not necessary when output parameters are properly configured. Carefully aligning parameter names and using the Value property avoids these problems.
Final Answer:
You should read the output parameter by name and use its Value property, for example: MessageBox.Show("Total is: " + cmd.Parameters["@ItemTotal"].Value.ToString());.
Discussion & Comments