Link to home
Start Free TrialLog in
Avatar of RadhaKrishnaKiJaya
RadhaKrishnaKiJaya

asked on

Calling Stored Procedure from C#

Hello Experts,
I am trying to call a Stored Procedure from C# code.  Every time I call, it is returning 0 (zero) rows.  The return value should '1' or '0'.  I checked in the database and found that my SP is not getting executed at all.  At the same time, I am not getting any error message in C# side.  Any idea why it is not giving any error message?  How would I know if the code did not execute the SP successfully.  Please look at the code and see if you can find the bug and correct the code if needed.  

Thank you very much in advance.


SqlTransaction objTrn;
SqlParameter[] parm;
SqlDataReader objSqlReader;
Boolean bChainStatus = false;
SqlConnection objCon = SqlHelper.GetConnection();
objCon.Open();
objTrn = objCon.BeginTransaction();

try
{
	XmlDocument objXml = new XmlDocument();
	objXml.LoadXml("<CHAIN_STATUS><SUB_TYPE/></CHAIN_STATUS>");

	parm = new SqlParameter[1];
	parm[0] = SqlHelper.CreatePerameter("@Chain_Num", SqlDbType.Int, 339, ParameterDirection.Input, -1, false);

	DataSet ds = new DataSet();
	ds = SqlHelper.ExecuteDataset(objTrn, CommandType.StoredProcedure, "ufn_ChainStatus", parm);

	if (ds.Tables.Count > 0)
	{
	    DataRow dr = ds.Tables[0].Rows[0];

	    objXml.DocumentElement.SelectSingleNode("SUB_TYPE").InnerText = dr["@SUB_TYPE"].ToString();
	    objTrn.Commit();
	    if (objCon.State != ConnectionState.Closed)
	    {
		objCon.Close();
	    }

	    bChainStatus = true;
	}
	else
	{
	    if (objCon.State != ConnectionState.Closed)
	    {
		objCon.Close();
	    }
	    bChainStatus = false;
	}
	}
	catch (Exception ex)
	{
	if (objCon.State != ConnectionState.Closed)
	{
	    objCon.Close();
	}
	bChainStatus = false;
}

return bChainStatus;

Open in new window

Avatar of zephyr_hex (Megan)
zephyr_hex (Megan)
Flag of United States of America image

have you tried putting a breakpoint here:

ds = SqlHelper.ExecuteDataset(objTrn, CommandType.StoredProcedure, "ufn_ChainStatus", parm);

Open in new window


and running debugger?  You should then be able to inspect the value of "parm" and potentially see other issues.

Also, SqlHelper is deprecated.  Is there a reason you're not using SqlCommand?  Usage:

using (var conn = new SqlConnection(connectionString))
using (var command = new SqlCommand("ProcedureName", conn) { 
                           CommandType = CommandType.StoredProcedure }) {
   conn.Open();
   command.Parameters.AddWithValue("@namedparameter", parametervalue);
   command.ExecuteNonQuery();
   conn.Close();
}

Open in new window

The problem is probably not in the code that you are showing us. We do not see the code that does the real job. The problem is either in your stored procedure or in the SqlHelper class.
Avatar of Carl Tawn
I'd agree with Jacques; if you're not getting an exception, and you're sure that the procedure is not being called, then that suggests that SqlHelper is either a) going wrong; or b) supressing the exception that is being raised.
Try running the Stored Procedure directly on your database to check if you are actually getting any data. If yes, then move on to debug your code.

From the naming convention, it seems that you are trying to execute a MSSQL User Defined Function rather than a Stored Procedure.

If indeed it is an SQL User Defined function then it has to be called as a SELECT QUERY rather than a STORED PROCEDURE.
ASKER CERTIFIED SOLUTION
Avatar of RadhaKrishnaKiJaya
RadhaKrishnaKiJaya

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The problem is either in your stored procedure or in the SqlHelper class. Looks like the author takes for himself something that was suggested 5 days ago. Telling him were to look for was the first step in solving the problem. Without seeing the code, we could not go all the way.
Avatar of RadhaKrishnaKiJaya
RadhaKrishnaKiJaya

ASKER

Already described the reasons.