Click here to Skip to main content
15,664,619 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Any Hints where the error could be while trying to call a stored project.It returns a null whereby it should contain a ID.

SqlConnection con = new SqlConnection(@"Data Source=FNUEXCPC01\SQLSERVER2005;Initial Catalog=Record_Management;Integrated Security=True");


SqlCommand cmdselect = new SqlCommand("RecordManagement_getcollegeid", con);
cmdselect.CommandType = CommandType.StoredProcedure;
cmdselect.CommandText = "RecordManagement_getcollegeid";

cmdselect.Parameters.Add(new SqlParameter("@College",ddlistschools.SelectedValue));
cmdselect.Parameters["@OutRes"].Direction = ParameterDirection.Output;

int Results;          
Results = (int)cmdselect.ExecuteScalar();

Results should contain a ID
Updated 8-Aug-12 14:10pm
Sergey Alexandrovich Kryukov 8-Aug-12 18:31pm    
ddlistschools.SelectedValue); will not compile... what are you showing here?
Member 9291223 8-Aug-12 18:54pm    
The stored procedure is like this:


ALTER PROCEDURE [dbo].[RecordManagement_getcollegeid]
@College nvarchar(max)
Select College_ID
From College
WHERE College_Name = @College
AspDotNetDev 8-Aug-12 20:15pm    
I am confused. I don't see an output parameter in that stored procedure, yet your C# code shows an output parameter. Please edit your question to include any code rather than posting it as a comment so we can all get a clear picture of what is going on.
Member 9291223 8-Aug-12 18:59pm    
THis statement is a commented like:

cmdselect.Parameters["@OutRes"].Direction = ParameterDirection.Output;

1 solution

I can't give specific help unless I see the stored procedure contents, but here are some ideas off the top of my head:
  • Maybe ExecuteScalar is returning SqlInt32 rather than int.
  • Maybe you are supposed to be getting the value of the output parameter.
  • Maybe @College is being passed in as the wrong data type (e.g., maybe an int is expected, but maybe you are passing in a string).
  • Maybe you did something wrong and there are no records to return from the stored procedure based on the inputs you gave it.

Some ways you can solve this:
  • Log any calls (e.g., to a different table) to the stored procedure so you can run them yourself from SQL Server Management Studio.
  • Use SQL Profiler to see exactly what is being passed to SQL Server.
  • Set some breakpoints to be sure all your values are as you expect (e.g., ddlistschools.SelectedValue).
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900