I'm using Visual Studio 2010 with database SQL
My c# code is :
public void Gettotal(int matreqid)
{
int total = 0;
try
{
sqlconnstring = ConfigurationManager.ConnectionStrings["CONERP"].ConnectionString;
sqlcon = new SqlConnection(sqlconnstring);
sqlcon.Open();
sqlcmd = new SqlCommand("GettotalMaterialRequisition", sqlcon);
sqlcmd.CommandType = CommandType.StoredProcedure;
sqlda.SelectCommand.Parameters.Add("@matreqid", SqlDbType.BigInt).Value = matreqid;
sqlcmd.Parameters.Add("@mmtotal", SqlDbType.BigInt).Direction = ParameterDirection.ReturnValue;
sqlcmd.ExecuteNonQuery();
total = Convert.ToInt32(sqlcmd.Parameters["@mmtotal"].Value);
sqlcmd.Dispose();
sqlcon.Close();
}
catch (SqlException sqlerr)
{
}
}
and SQL stored procedure is
ALTER procedure [dbo].[GettotalMaterialRequisition](@mmtotal bigint OUTPUT, @matreqid bigint)
as
begin
set @mmtotal = (select( sum (rate * qty * nooflab)) from MaterialRequisitionList where (matreqid = @matreqid and chkmr = 1))
RETURN @mmtotal;
end
You are executing a stored procedure that expects two parameters, but you pass just one parameter through the command
sqlcmd = new SqlCommand("GettotalMaterialRequisition", sqlcon);
sqlcmd.CommandType = CommandType.StoredProcedure;
sqlcmd.Parameters.Add("@matreqid", SqlDbType.BigInt).Value = matreqid;
sqlcmd.Parameters.Add("@mmtotal", SqlDbType.BigInt).Direction = ParameterDirection.ReturnValue;
sqlcmd.ExecuteNonQuery();
The second parameter is added to the select command of a dataadapter. That adapter is not involved in the call to the stored procedure.
Also, I hope that you don't really swallow the exception like you show in the code above. Catching an exception and then doing nothing with it is a very bad practice and could hide obvious errors
This bit is incorrect:
sqlcmd.Parameters.Add("@mmtotal", SqlDbType.BigInt).Direction =
ParameterDirection.ReturnValue;
You instead want:
sqlcmd.Parameters.Add("@mmtotal", SqlDbType.BigInt).Direction =
ParameterDirection.Output;
(And as @Steve points out, this line should also change:
sqlda.SelectCommand.Parameters.Add("@matreqid", SqlDbType.BigInt).Value = matreqid;
should be:
sqlcmd.Parameters.Add("@matreqid", SqlDbType.BigInt).Value = matreqid;
)
You can then remove the following line from your stored procedure:
RETURN @mmtotal;
The return value from a stored procedure is always of type int
. You may have been truncating your value by trying to put a bigint
in there.
As a stylistic matter, I'd also recommend putting your OUTPUT
parameter(s) last in the formal parameter list for the procedure, but that's just conventional, not required.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.