---
title: "Error on executing a stored procedure using While condition"  
description: "Error on executing a stored procedure using While condition"  
author: "Pravesh Singh"  
published: 2013-04-02  
updated: 2013-04-02  
canonical: https://www.mindstick.com/forum/697/error-on-executing-a-stored-procedure-using-while-condition  
category: "c#"  
tags: ["c#", ".net", "mindstick", "free open source", "Error on executing", "stored procedure", "using While condition"]  
reading_time: 1 minute  

---

# Error on executing a stored procedure using While condition

\
Hi [Expert](https://www.mindstick.com/articles/13120/an-expert-financial-advice-will-improve-your-finances)!\
my line of code as below\
[SqlConnection](https://www.mindstick.com/forum/1209/sqlconnection-sqlcommand-sqldatareader-idisposable) sqlConn = new SqlConnection(MyClass.GlobalConn()); sqlConn.Open();\
try { [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) getAllBranch = "[SELECT](https://www.mindstick.com/forum/160534/orderby-then-select-vs-select-then-orderby-performance) iBranch_num,LTRIM(RTRIM(sConstant)) FROM tblgobranch"; [SqlCommand](https://www.mindstick.com/forum/91/difference-between-sqlcommand-and-sqlcommandbuilder) cmdgetAllBranch = new SqlCommand(getAllBranch, sqlConn); SqlDataReader dRgetAllBranch=cmdgetAllBranch.ExecuteReader();\
\
\
while (dRgetAllBranch.Read()) {\
SqlCommand cmd = new SqlCommand("dbo.rvk_GetSalesPerItem", sqlConn); cmd.CommandType = CommandType.StoredProcedure;\
cmd.Parameters.Add("@piDateFrom", SqlDbType.Int).Value = FromDT1; cmd.Parameters.Add("@piDateThru", SqlDbType.Int).Value = ToDT2; cmd.Parameters.Add("@[BRANCH](https://answers.mindstick.com/qa/41075/why-did-the-continental-congress-adopt-the-olive-branch-petition)", SqlDbType.NVarChar).Value = dRgetAllBranch[1].ToString(); cmd.Parameters.Add("@brNum", SqlDbType.Int).Value = dRgetAllBranch[0].ToString(); cmd.Parameters.Add("@PluCode", SqlDbType.NVarChar).Value = str1; cmd.ExecuteNonQuery();\
}I got [error](https://yourviews.mindstick.com/view/88527/fixing-quickbooks-error-4120-reinstalling-vs-repairing) here\
"There is [already](https://yourviews.mindstick.com/view/88453/the-hidden-ways-ai-is-already-controlling-your-daily-life) open [data](https://www.mindstick.com/articles/13050/salesforce-aiming-to-dominate-predictive-analytics-with-data-science) reader associated with this [command](https://www.mindstick.com/blog/178/synchronous-and-asynchronous-command-execution-in-c-sharp-dot-net)"\
Please help me with this.

## Replies

### Reply by AVADHESH PATEL

Hi Pravesh!\
Try this way\

```
SqlConnection sqlConnStoredProc = new SqlConnection(MyClass.GlobalConn());sqlConnStoredProc.Open();SqlCommand cmd = new SqlCommand("dbo.rvk_GetSalesPerItem", sqlConnStoredProc);cmd.CommandType = CommandType.StoredProcedure;cmd.Parameters.Add("@piDateFrom", SqlDbType.Int).Value = FromDT1;cmd.Parameters.Add("@piDateThru", SqlDbType.Int).Value = ToDT2;cmd.Parameters.Add("@BRANCH", SqlDbType.NVarChar).Value = dRgetAllBranch[1].ToString();cmd.Parameters.Add("@brNum", SqlDbType.Int).Value = dRgetAllBranch[0].ToString();cmd.Parameters.Add("@PluCode", SqlDbType.NVarChar).Value = str1;cmd.ExecuteNonQuery();sqlConnStoredProc.Close();
```

\
I hope it resolve your problem


---

Original Source: https://www.mindstick.com/forum/697/error-on-executing-a-stored-procedure-using-while-condition

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
