Home > DeveloperSection > Forums > Error on executing a stored procedure using While condition
Pravesh Singh

Total Post:411

Points:2881
Posted on    April-02-2013 2:29 AM

 C#
Ratings:


 1 Reply(s)
 749  View(s)
Rate this:

Hi Expert!

my line of code as below

SqlConnection sqlConn = new SqlConnection(MyClass.GlobalConn());
        sqlConn.Open();

        try
        {
            string getAllBranch = "SELECT iBranch_num,LTRIM(RTRIM(sConstant)) FROM tblgobranch";
            SqlCommand 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", 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 here

"There is already open data reader associated with this command"

Please help me with this.


AVADHESH PATEL

Total Post:604

Points:4228
Posted on    April-02-2013 7:34 AM

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

Don't want to miss updates? Please click the below button!

Follow MindStick