J
jason
on SQL Server i have the following sample stored procedure:
CREATE PROC owner.procname (
@id int,
@desc varchar(50) output)
AS
select @desc = description
from table1 where id = @id
in a class library, i have the following two methods:
public string GetDescription(int nID, string sConnectString)
{
SqlConnection oConn = new SqlConnection(sConnectString);
oConn.Open();
SqlCommand oCommand = oConn.CreateCommand();
oCommand.CommandType = CommandType.StoredProcedure;
oCommand.CommandText = "procname";
AddParam(oCommand, "@id", SqlDbType.Int,
ParameterDirection.Input, nID);
AddParam(oCommand, "@desc", SqlDbType.VarChar,
ParameterDirection.Output, null);
oCommand.ExecuteNonQuery();
string sReturn = (string) oCommand.Parameters["@desc"].Value;
oCommand.Dispose();
oConn.Close();
oConn.Dispose();
return sReturn;
}
public void AddParam(SqlCommand oCommand, string sName, SqlDbType
eType, ParameterDirection eDirection, object oValue)
{
SqlParameter oParam = oCommand.Parameters.Add(sParamName, eType);
oParam.Direction = eDirection;
if (oValue == null)
{
switch (eType)
{
case SqlDbType.VarChar:
oParam.Value = "";
oParam.Size = 0;
break;
case SqlDbType.Bit:
case SqlDbType.Int:
case SqlDbType.Money:
oParam.Value = 0;
break;
case SqlDbType.DateTime:
oParam.Value = DateTime.Now;
break;
}
}
else if (eType == SqlDbType.VarChar)
{
oParam.Value = oValue;
string sValue = (string) oValue;
oParam.Size = sValue.Length;
}
}
the problem is when i instatiate the class, and run the GetDescription
method, i receive the following error:
Procedure 'procname' expects parameter '@id', which was not supplied.
but the @id parameter IS supplied, unless i'm doing it incorrectly with
the Add method of the parameter collection?
thanks for any help figuring out why the stored procedure thinks i
haven't supplied the @id parameter,
jason
CREATE PROC owner.procname (
@id int,
@desc varchar(50) output)
AS
select @desc = description
from table1 where id = @id
in a class library, i have the following two methods:
public string GetDescription(int nID, string sConnectString)
{
SqlConnection oConn = new SqlConnection(sConnectString);
oConn.Open();
SqlCommand oCommand = oConn.CreateCommand();
oCommand.CommandType = CommandType.StoredProcedure;
oCommand.CommandText = "procname";
AddParam(oCommand, "@id", SqlDbType.Int,
ParameterDirection.Input, nID);
AddParam(oCommand, "@desc", SqlDbType.VarChar,
ParameterDirection.Output, null);
oCommand.ExecuteNonQuery();
string sReturn = (string) oCommand.Parameters["@desc"].Value;
oCommand.Dispose();
oConn.Close();
oConn.Dispose();
return sReturn;
}
public void AddParam(SqlCommand oCommand, string sName, SqlDbType
eType, ParameterDirection eDirection, object oValue)
{
SqlParameter oParam = oCommand.Parameters.Add(sParamName, eType);
oParam.Direction = eDirection;
if (oValue == null)
{
switch (eType)
{
case SqlDbType.VarChar:
oParam.Value = "";
oParam.Size = 0;
break;
case SqlDbType.Bit:
case SqlDbType.Int:
case SqlDbType.Money:
oParam.Value = 0;
break;
case SqlDbType.DateTime:
oParam.Value = DateTime.Now;
break;
}
}
else if (eType == SqlDbType.VarChar)
{
oParam.Value = oValue;
string sValue = (string) oValue;
oParam.Size = sValue.Length;
}
}
the problem is when i instatiate the class, and run the GetDescription
method, i receive the following error:
Procedure 'procname' expects parameter '@id', which was not supplied.
but the @id parameter IS supplied, unless i'm doing it incorrectly with
the Add method of the parameter collection?
thanks for any help figuring out why the stored procedure thinks i
haven't supplied the @id parameter,
jason