5
Answers

getting connection error when i am calling function inside

Photo of Sk Jha

Sk Jha

7y
142
1
getting error "There is already an open DataReader associated with this Command which must be closed first.The connection was not closed. The connection's current state is open."
 
Please any one can help me to solve the error.
 
here is my code
  1. double totalExchange=0, totalBuyBack=0;  
  2. double markup=0;  
  3. void getData()  
  4. {  
  5. if (txtLotNumber.Text != "")  
  6. {  
  7. string total = "";  
  8. try  
  9. {  
  10. string com = "select * from PriceBreakupSaleBuyBack where itemNumber=@itemNumber;";  
  11. com += "select G_From, G_To, value from MarkupSlab where GorP='G'";  
  12. using (SqlCommand cmd = new SqlCommand(com, con))  
  13. {  
  14. cmd.Parameters.AddWithValue("@itemNumber", txtLotNumber.Text);  
  15. con.Open();  
  16. using (SqlDataReader dr = cmd.ExecuteReader())  
  17. {  
  18.   
  19. while (dr.Read())  
  20. {  
  21. lblLotNumber.Text = dr["itemNumber"].ToString();  
  22. lblDescription.Text = dr["DesignDescription"].ToString();  
  23. lblGrossWt.Text = dr["GrossWeight"].ToString();  
  24. lblDesign.Text = dr["design"].ToString();  
  25. lblDiamondWt.Text = dr["DiamondWeight"].ToString();  
  26. lblGoldWt.Text = dr["GoldWeight"].ToString();  
  27. total = dr["itemDescription"].ToString();  
  28. if (total == "Total")  
  29. {  
  30. totalBuyBack += Convert.ToDouble(dr["Amount"].ToString());  
  31. totalExchange += Convert.ToDouble(dr["excAmount"].ToString());  
  32.   
  33. }  
  34. }  
  35. double diaWt = Convert.ToDouble(lblDiamondWt.Text);  
  36. if (diaWt > 0.25)  
  37. {  
  38. lblTotalBuyBk.Text = "" + totalBuyBack;  
  39. lblTotalEx.Text = "" + totalExchange;  
  40. fillGrid();  
  41. note();  
  42. }  
  43.   
  44. else  
  45. {  
  46. if (dr.NextResult())  
  47. {  
  48. while (dr.Read())  
  49. {  
  50. double Gfr = Convert.ToDouble(dr["G_From"].ToString());  
  51. double Gto = Convert.ToDouble(dr["G_To"].ToString());  
  52. if (totalExchange >= Gfr && totalExchange <= Gto)  
  53. {  
  54. markup = Convert.ToDouble(dr["value"].ToString());  
  55. }  
  56. }  
  57. }  
  58. con.Close();  
  59. totalExchange *= 1.25;  
  60. totalBuyBack *= 1.25;  
  61. totalExchange += totalExchange * markup / 100;  
  62. totalBuyBack += totalBuyBack * markup / 100;  
  63. lblTotalBuyBk.Text = "" + totalBuyBack;  
  64. lblTotalEx.Text = "" + totalExchange;  
  65. note();  
  66. }  
  67. }  
  68. }  
  69. }  
  70. catch (Exception ex)  
  71. {  
  72. Response.Write(ex.Message);  
  73. }  
  74. }  
  75. else  
  76. {  
  77. Response.Write("<!-- Inject Script Filtered -->");  
  78. }  
  79. }  
  80. void fillGrid()  
  81. {  
  82. try  
  83. {  
  84. string com = "select itemGroup, Item, ItemDescription, Pieces, Weight, ExcAmount, Amount from PriceBreakupSaleBuyBack";  
  85. SqlCommand cmd = new SqlCommand(com, con);  
  86. SqlDataAdapter da = new SqlDataAdapter(cmd);  
  87. DataSet ds = new DataSet();  
  88. da.Fill(ds);  
  89. GridView1.DataSource = ds;  
  90. GridView1.DataBind();  
  91. string data;  
  92. for (int i = 0; i < (GridView1.Rows.Count); i++)  
  93. {  
  94. data=GridView1.Rows[i].Cells[0].Text.ToString();  
  95. data = data.Split(' ').First();  
  96. GridView1.Rows[i].Cells[0].Text = data;  
  97. }  
  98. if (GridView1.Rows.Count > 0)  
  99. {  
  100. GridView1.HeaderRow.Cells[0].Text = "Type";  
  101. GridView1.HeaderRow.Cells[3].Text = "Pcs/KT";  
  102. GridView1.HeaderRow.Cells[4].Text = "Wt";  
  103. }  
  104. color();  
  105. }  
  106. catch (Exception ex) { Response.Write(ex.Message); }  
  107. }  
  108. void color()  
  109. {  
  110. string data;  
  111. for (int i = 0; i < (GridView1.Rows.Count); i++)  
  112. {  
  113. data = GridView1.Rows[i].Cells[2].Text.ToString();  
  114. if (data.Equals("Total"))  
  115. {  
  116. GridView1.Rows[i].ForeColor = Color.Red;  
  117. }  
  118.   
  119. }  
  120.   
  121. }  
  122. void note()  
  123. {  
  124. string note="";  
  125. string com = "select * from PriceBreakupBuyBackNotes";  
  126. SqlCommand cmd = new SqlCommand(com, con);  
  127. con.Open();  
  128. SqlDataReader dr;  
  129. dr = cmd.ExecuteReader();  
  130. while (dr.Read())  
  131. {  
  132. note += dr["id"].ToString()+". ";  
  133. note += dr["note"].ToString()+"
    "
    ;  
  134. }  
  135. lblNote.Text = note;  
  136. lblNote.ForeColor = Color.Red;  
  137. con.Close();  
  138. }  

Answers (5)

1
Photo of Prasham Sabadra
NA 17.6k 1.2m 8y
Hi Venkat,
Please try this
<script src=”/_layouts/SP.js” type=”text/ecmascript”></script> 
Accepted
1
Photo of Ramesh Palaniappan
NA 13.2k 722.6k 8y
Please add this line to call your function
  1. ExecuteOrDelayUntilScriptLoaded(myjsfucntion,"sp.js");