Click here to Skip to main content
15,921,351 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
Dear Team,
I have a stored procedure...below the code of stored procedure in SQL.....I have googled but not any statisfied result and I tried to solve itself but could not solve and finaly i m helpless to ask the code project team.....this stored procedure is working fine without using linq.I have given small snippet of my code as follows
SQL
ALTER PROCEDURE [dbo].[SP_Get_Next_Id] 
	(
		@Table_Name varchar(40),
		@Column_Name varchar(40),  --From which column to get the MaxId
		@SQL_Ret int output
	)
AS
Declare @SQL_MAX nVarchar(100)
DECLARE @ParmDefinition NVARCHAR(500)

BEGIN
	SET NOCOUNT ON
	Begin
		set @SQL_MAX='Select @SQL_MAX_OUT=isnull(Max('+@Column_Name+'),0)+1 From '+@Table_Name
		
		SET @ParmDefinition = N'@SQL_MAX_OUT varchar(30) OUTPUT'

		exec sp_executesql @SQL_MAX,@ParmDefinition,@SQL_MAX_OUT=@SQL_Ret OUTPUT
	End
END

-------LINQ MAPPING----
C#
using System.Data;
using System.Configuration;
using System.Linq;
using System.Data.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Linq.Mapping;
using System.Reflection;
public class DB_DataContext:DataContext
{
    public DB_DataContext()
        : base(@"Data source=.\SQLEXPRESS;AttachDbFileName=|Datadirectory|\Data\Fleet_CMMS.MDF;
                             Integrated Security=True;User Instance=True")
    {}
    

    [Function(Name = "dbo.SP_Get_Next_Id")]
    public ISingleResult<int> SP_Get_Next_Id([Parameter(DbType = "Varchar(40)")] string TableName,
               [Parameter(DbType = "Varchar(40)")] string Column_Name,
                [Parameter(DbType = "int")] ref int SQL_Ret)
    {
        IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), TableName, Column_Name, SQL_Ret);
        return (ISingleResult<int>)(result.ReturnValue);
    }
}


----CALLING CODE----
C#
int return_variable=0;
DB_DataContext Db_dc = new DB_DataContext();
Db_dc.SP_Get_Next_Id("CMMS_DEPOT_MASTER", "Depot_ID", ref return_variable);


IT GIVES ERROR
System.InvalidOperationException: The type 'System.Int32' must declare a default (parameterless) constructor in order to be constructed during mapping. at System.Data.Linq.SqlClient.Translator.BuildProjectionInternal(SqlExpression item, MetaType rowType, IEnumerable`1 members, Boolean allowDeferred, SqlLink link, Expression source) at System.Data.Linq.SqlClient.Translator.BuildProjection(SqlExpression item, MetaType rowType, Boolean allowDeferred, SqlLink link, Expression source) at System.Data.Linq.SqlClient.QueryConverter.TranslateStoredProcedureCall(MethodCallExpression mce, MetaFunction function) at System.Data.Linq.SqlClient.QueryConverter.VisitMappedFunctionCall(MethodCallExpression mc) at System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc) at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node) at System.Data.Linq.SqlClient.QueryConverter.ConvertOuter(Expression node) at System.Data.Linq.SqlClient.SqlProvider.BuildQuery(Expression query, SqlNodeAnnotations annotations) at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query) at System.Data.Linq.DataContext.ExecuteMethodCall(Object instance, MethodInfo methodInfo, Object[] parameters) at DB_DataContext.SP_Get_Next_Id(String TableName, String Column_Name, Int32& SQL_Ret) in d:\sukhen\Projects\ASP.NET\Fleet_CMMS\App_Code\DB_DataContext.cs:line 34 at Forms_frmDepot.btnSave_Click(Object sender, EventArgs e) in d:\sukhen\Projects\ASP.NET\Fleet_CMMS\Forms\frmDepot.aspx.cs:line 68


[edit]SHOUTING removed - OriginalGriff[/edit]
Posted
Updated 8-Oct-11 21:41pm
v6
Comments
OriginalGriff 9-Oct-11 3:41am    
DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalisation if you want to be taken seriously.

Change the line
int return_variable=0;

to
int? return_variable = 0;


This should resolve the issue, if not please let us know at which point you are getting this error message ?
 
Share this answer
 
Comments
sukhen dass 10-Oct-11 8:25am    
Hi,bala thank u to give your precious time...I tried as per your solution but the same error msg and I changed in variable declaration "int? return_variable = 0;" and in the defined function "ref int? SQL_Ret" parameters as per the requirement to define the nullable
and error is at this line
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), TableName, Column_Name, SQL_Ret);
sukhen dass 12-Oct-11 5:28am    
Bala pls,reply
Bala Selvanayagam 12-Oct-11 7:24am    
I will have an another look today and come back to you
Bala Selvanayagam 12-Oct-11 10:50am    
Replace your function by

[global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.SP_Get_Next_Id")]
public int SP_Get_Next_Id([global::System.Data.Linq.Mapping.ParameterAttribute(Name="Table_Name", DbType="VarChar(40)")] string table_Name, [global::System.Data.Linq.Mapping.ParameterAttribute(Name="Column_Name", DbType="VarChar(40)")] string column_Name, [global::System.Data.Linq.Mapping.ParameterAttribute(Name="SQL_Ret", DbType="Int")] ref System.Nullable<int> sQL_Ret)
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), table_Name, column_Name, sQL_Ret);
sQL_Ret = ((System.Nullable<int>)(result.GetParameterValue(2)));
return ((int)(result.ReturnValue));
}


and then int? return_variable = 0;

let me know the results
sukhen dass 13-Oct-11 5:39am    
Hi,Bala thanks for your solution its working....But how to return string value rather than integer is it possible
-----step1 in sql server 2005-----

CREATE PROCEDURE [dbo].[SP_Get_Next_Id1]
(
@Table_Name varchar(40),
@Column_Name varchar(40), --From which column to get the MaxId
@SQL_Ret VARCHAR(40) output
)
AS
Declare @SQL_MAX nVarchar(100)
DECLARE @ParmDefinition NVARCHAR(500)
--DECLARE @Return_Value int
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON
Begin
--print 'Seelct Max('+@Column_Name+')+1 From '+@Table_Name
set @SQL_MAX='Select @SQL_MAX_OUT=isnull(Max('+@Column_Name+'),0)+1 From '+@Table_Name
--set @SQL_Ret=EXEC(@SQL_MAX)
SET @ParmDefinition = N'@SQL_MAX_OUT varchar(40) OUTPUT'
exec sp_executesql @SQL_MAX,@ParmDefinition,@SQL_MAX_OUT=@SQL_Ret OUTPUT
End
END

-----step2 in VS2008 and framework 3.5 ----

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Data.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Linq.Mapping;
using System.Reflection;
using System.Windows.Forms;
using System.Data.Linq.Mapping;

public class DB_DataContext:DataContext
{
public DB_DataContext()
: base(@"Data source=.\SQLEXPRESS;AttachDbFileName=|Datadirectory|\Data\Fleet_CMMS.MDF;
Integrated Security=True;User Instance=True")
{}

[global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.SP_Get_Next_Id1")]
public string SP_Get_Next_Id1([global::System.Data.Linq.Mapping.ParameterAttribute(Name="Table_Name", DbType="VarChar(40)")] string table_Name, [global::System.Data.Linq.Mapping.ParameterAttribute(Name="Column_Name", DbType="VarChar(40)")] string column_Name, [global::System.Data.Linq.Mapping.ParameterAttribute(Name="SQL_Ret", DbType="VarChar(40)")] ref string SQL_Ret)
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), table_Name, column_Name, SQL_Ret);
SQL_Ret = Convert.ToString((result.GetParameterValue(2)));
return ((string)(result.ReturnValue));
}
}


----step3-----calling----
DB_DataContext Db_dc = new DB_DataContext();
String strReturn_Var = "";
Db_dc.SP_Get_Next_Id1("CMMS_DEPOT_MASTER", "Depot_ID", ref strReturn_Var);
//in the database Depot_Id is an autonumber and integer column but i test it with varchar columns as well but same error msg.
 
Share this answer
 
Comments
sukhen dass 17-Oct-11 4:22am    
Actually I want "string" value as a return not the "int" value as string.If I return an Int value as string is working but in actual I want the string value as return...For ex.I have a column Id but it has Varchar Value(AI01)
.If its an integer column and values are(1,2,3.....n) its working but varchar value not working...

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