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

RadhaKrishnaKiJayaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zephyr_hex (Megan)DeveloperCommented:
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

0
Jacques Bourgeois (James Burger)PresidentCommented:
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.
0
Carl TawnSystems and Integration DeveloperCommented:
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.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Athar SyedCommented:
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.
0
RadhaKrishnaKiJayaAuthor Commented:
Hello Guys,
Thank you for all your help.  The real issue was with the Stored Procedure, where I was using RETURN statement.  I just changed it to SELECT CHAIN_STATUS AS 'CHAINSTATUS' and it worked fine.  Again Thank you for all your tips.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jacques Bourgeois (James Burger)PresidentCommented:
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.
0
RadhaKrishnaKiJayaAuthor Commented:
Already described the reasons.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.