Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have created a Stored procedure in SQL Server database which returns just 1 record of 4 columns all four columns are of integer data type and one of them is Identity(1,1) column

My sql code is as below
CREATE PROCEDURE [dbo].[GetDecrementValue]
	@Ref_AccPeriod_ID INT,
	@Ref_LeaveType_ID INT,
	@Ref_LeaveTime_ID INT,
	@ResponseText VARCHAR(MAX) OUTPUT
AS
BEGIN
	SET NOCOUNT ON;

	IF EXISTS(SELECT * FROM Decrement WHERE Ref_AccPeriod_ID = @Ref_AccPeriod_ID AND Ref_LeaveType_ID = @Ref_LeaveType_ID AND Ref_LeaveTime_ID = @Ref_LeaveTime_ID AND IsActive = 1)
		BEGIN
			SELECT			 
			DecrementType,
			DecrementValue,
			IsActive,
			Ref_Decrement_ID
			FROM Decrement 
			WHERE Ref_AccPeriod_ID = @Ref_AccPeriod_ID 
			AND Ref_LeaveType_ID = @Ref_LeaveType_ID
			AND Ref_LeaveTime_ID = @Ref_LeaveTime_ID
			AND IsActive = 1
		END
	ELSE
		BEGIN
			SET @ResponseText = 'Leave Decrement Value not found.'
		END
END


I have tested the Sql code it work fine and give correct output.
And have written a c# code using SqlParameters and SqlDataAdapter to fetch the data returned by the Stored Procedure.
This is where the problem is. It just returns and Empty DataSet.

This is My Code from the DataStore.cs Class file which is the place where the common DataAccess Code is written
C#
public DataSet GetDataDSWithParameters(string ProcedureName, SqlParameter[] param)
{
   DataSet getDataSet = new DataSet();
   try
   {                
     LTConn = new SqlConnection();
     LTConn.ConnectionString = connection;
     LTConn.Open();                
     LTCmd = new SqlCommand();
     LTCmd.Connection = LTConn;
     LTCmd.CommandType = CommandType.StoredProcedure;
     LTCmd.CommandText = ProcedureName;
     getDapt = new SqlDataAdapter();
     getDapt.SelectCommand = new SqlCommand(LTCmd.CommandText, LTConn);
     getDapt.SelectCommand.CommandType = CommandType.StoredProcedure;
     getDapt.SelectCommand.Parameters.AddRange(param);
     getDapt.Fill(getDataSet);
     LTConn.Close();
   }
   catch (Exception) { }
   return getDataSet;
}


This is the DataAccessLayer code which call the above method
C#
public DataSet UserGetLeaveDecrementValueDA(LeaveDecrementBO ObjLeaveDecrementBO)
{
  SqlParameter[] GetParameter = new SqlParameter[4];
  GetParameter[0] = new SqlParameter("@Ref_AccPeriod_ID", SqlDbType.Int);
  GetParameter[0].Value = ObjLeaveDecrementBO.Ref_AccountingPeriod_ID;
  GetParameter[0].Direction = ParameterDirection.Input;

  GetParameter[1] = new SqlParameter("@Ref_LeaveType_ID", SqlDbType.Int);
  GetParameter[1].Value = ObjLeaveDecrementBO.Ref_LeaveType_ID;
  GetParameter[1].Direction = ParameterDirection.Input;

  GetParameter[2] = new SqlParameter("@Ref_LeaveTime_ID", SqlDbType.Int);
  GetParameter[2].Value = ObjLeaveDecrementBO.Ref_LeaveTime_ID;
  GetParameter[2].Direction = ParameterDirection.Input;

  GetParameter[3] = new SqlParameter("@ResponseText", SqlDbType.VarChar, 8000);
  GetParameter[3].Direction = ParameterDirection.Output;
  return objDataStore.GetDataDSWithParameters("GetDecrementValue", GetParameter);
}

This method above is called at the Asp Web Page Level.
Any help is appreciated.
Posted
Updated 25-Apr-15 4:38am
v4
Comments
Tomas Takac 25-Apr-15 9:45am    
If the SP executes just fine in management studio then the problem must be on the other side - in your C# code. Perhaps you should show that as well.
Christopher Fernandes 25-Apr-15 10:35am    
I have provided the code now. Please check it.

1 solution

My guess is there is an exception but it gets swallowed in the empty catch block. Your code then happily goes on to return the dataset which was created at the beginning of the method and is of course empty. You should never ignore exceptions like that. Always catch only exceptions you can handle! For now just remove the try-catch altogether and see what error you are getting.
 
Share this answer
 
Comments
Christopher Fernandes 25-Apr-15 12:33pm    
I tried doing that but no luck. The method GetDataDSWithParameters in the DataStore.cs class file is used by many other page and works fine there but just not for this call.
Tomas Takac 25-Apr-15 13:27pm    
No exception, really? Try to use SQL server profiler to get the exact command that is executed, then run it in management studio and see if it returns data. Just to make sure it's running with the parameters you think it is. I have no other idea at this point.
Tomas Takac 25-Apr-15 13:31pm    
BTW did you check the value of response text after execution?
Christopher Fernandes 25-Apr-15 13:45pm    
Thanks a ton!!!! using the profiler worked like magic. Solved my Issue.

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