Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
In my data layer, I have a method NewCaseNumber to create a system-gen number using stored proc. I get error converting datatype varchar to int. My caseType table has SP-ComplexCrimes. But Im only retreiving the SP portion.
This storedProc generates 2015SP00001. I tested it and it executes fine in db
SQL
ALTER PROCEDURE [dbo].[GetNewCaseNumber]  
	-- Add the parameters for the stored procedure here
	@DateOpened as datetime,
	@CaseType as int,
	@NewIDOUT varchar(12)	OUTPUT

AS
BEGIN
SET NOCOUNT ON;

	-- Declare the return variable here
	DECLARE @InvType char(2)
	DECLARE @NewID varchar(12)
	DECLARE @NextNum char(5)

	-- Add the T-SQL statements to compute the return value here
	SELECT @InvType = left(Description,2)
		FROM CaseTypeList
		WHERE ID = @CaseType

	SELECT @NextNum = right('00000' + cast(isnull(max(cast(right(i.InvestigationNum,5) as int)),0) + 1 as varchar(5)),5)
		FROM Investigation i inner join CaseTypeList c on i.CaseType = c.ID
		WHERE substring(i.InvestigationNum,5,2) = @InvType and left(i.InvestigationNum,4) = Year(@DateOpened)

	SELECT @NewID = cast(Year(@DateOpened) as char(4)) + @InvType + @NextNum 

	-- Return the result of the function
	SET @NewIDOUT = @NewID;
END

In my DAL, error is from the ExecuteScalar line
C#
public static string GetNewCaseNumber(DateTime DateOpened, Int32 caseType, Int32 NewIDOUT)
	{
		Database db = DatabaseFactory.CreateDatabase();
		DbCommand dbCommand = db.GetStoredProcCommand("GetNewCaseNumber");

		db.AddInParameter(dbCommand, "@DateOpened", DbType.DateTime, DateTime.Now.Date);
		db.AddInParameter(dbCommand, "@CaseType", DbType.Int32, caseType);
		db.AddOutParameter(dbCommand, "@NewIDOUT", DbType.Int32, NewIDOUT);
		
	return (string)db.ExecuteScalar(dbCommand);
	
	}

Modified to below
SQL
public static string GetNewCaseNumber(DateTime DateOpened, Int32 caseType, Int32 NewIDOUT)
	{
        int Result;

        Database db = DatabaseFactory.CreateDatabase();
        DbCommand dbCommand = db.GetStoredProcCommand("GetNewCaseNumber");

	  db.AddInParameter(dbCommand, "@DateOpened", DbType.DateTime, DateTime.Now.Date);
	  db.AddInParameter(dbCommand, "@CaseType", DbType.Int32, caseType);
	  db.AddOutParameter(dbCommand, "@NewIDOUT", DbType.Int32, NewIDOUT);

        db.ExecuteNonQuery(dbCommand);

        Result = (Int32)db.GetParameterValue(dbCommand, "@NewIDOUT");


//New error: Not all code paths return a value

I need help doing it properly, co-worker suggested using ExecuteNonQuery, but I need to return it as a string 2015SP00001. Any help is appreciated, or just point me in the right direction. Thank you!
Posted
Updated 5-Mar-15 8:43am
v3
Comments
Sergey Alexandrovich Kryukov 4-Mar-15 18:25pm    
Why? If some data is integer data, it should use some numeric type.
—SA
George Jonsson 4-Mar-15 22:17pm    
Not clear what you want to do.
You have this string, '2015SP00001'. Do you want to convert part of it to an integer?
Or do you want to use Base36 and convert the whole string to an integer?
Sam 9100 4-Mar-15 23:01pm    
George, the 2015SP00001 s/b the NewCaseNumber/final outcome once saved. I wanted to pass it as a string, but if I need to return as int, I need help with syntax, also may change the Public static int GetNewCaseNumber?
Sam 9100 4-Mar-15 23:06pm    
The error is either int CaseType? or NewIDOUT varchar? Do I need to convert.ToInt32 somewhere?
Tushar sangani 4-Mar-15 23:48pm    
i think the error in the DECLARE @NextNum char(5)

DECLARE @NextNum int

Not seeing the actual implementations if AddInParameter and AddOutParameter it looks like the problem is in
C#
db.AddOutParameter(dbCommand, "@NewIDOUT", DbType.Int32, NewIDOUT);

Should it be
C#
db.AddOutParameter(dbCommand, "@NewIDOUT", DbType.String, NewIDOUT);

The same applies to the parameters of your method and it probably should be marked as out.
 
Share this answer
 
Comments
Sam 9100 5-Mar-15 14:35pm    
I tried that it gave different error, cannot convert int to string. So I modified my GetNewCaseNumber (above), now it says not all code paths return a value.
Wendelius 5-Mar-15 23:27pm    
You haven't posted the whole method but as far as there is code, it seems that you're missing the return statement.
Sam 9100 6-Mar-15 10:32am    
that is all the code I have for the GetNewCaseNumber in my DAL, and the is the stored procedure. Once I get this working, then I do what I need to in the BLL and in the UI.
I replaced the result line with returning the output and specified the size of return data, and it worked.
return (string)db.GetParameterValue(dbCommand, "@NewIDOUT");
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 29-Apr-15 19:34pm    
Sorry, this is not an answer. Such posts are considered as abuse. You could simply informs the readers about that in your question, using "Improve question". I would advise to remove this post, to avoid abuse reports.
—SA

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