Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Error:-invalid attempt to call read when reader is closed c#
My code Following:

C#
while (dr.Read())
           {

                List.Add(dr["LocationNo"]);


                   if (vehno != dr["vehicleassign"].ToString())
                   {
                       if (vehno == "")
                       {
                           st.Append("<table style="\"" mode="hold" />                            st.Append("<tr><th>Vehicle No.</th><th>Location Name</th><th>Area</th><th>Location No</th><th>Total Lifts</th><th>Bin Weight</th></tr>");
                           st.Append("<tr>");
                           st.Append("<td style="\"" mode="hold" />                        }
                       else
                       {
                           st.Append("");
                           st.Append("<br />");
                           st.Append("<table style="\"" mode="hold" />                            st.Append("<tr><th>Vehicle No.</th><th>Location Name</th><th>Area</th><th>Location No.</th><th>Total Lifts</th><th>Bin Weight</th></tr>");
                           st.Append("<tr>");
                           st.Append("<td style="\"" mode="hold" />                        }
                       vehno = dr["vehicleassign"].ToString();
                   }
                   else
                   {
                       st.Append("<td style="\"" mode="hold" />                    }
                   st.Append("<td style="\"" mode="hold" />                    st.Append("<td style="\"" mode="hold" />

                   string qry1 = "select locationNo,vehicleno, count(binweight) as Totallift ,sum(binweight) as TotalWeight   from DailyBinAction  where recivedatetime >='2-9-2015 00:00:00 AM' and  recivedatetime <= '3-10-2015 23:59:59 PM' group by locationNo , vehicleno";
                   SqlCommand cmd1 = new SqlCommand(qry1, con);
                   cmd1.CommandType = CommandType.Text;
                   SqlDataAdapter adp = new SqlDataAdapter(cmd1);
                   DataTable dt = new DataTable();
                   adp.Fill(dt);
                   // SqlDataReader dr1 = cmd1.ExecuteReader();
                   con.Close();
                   foreach (DataRow item in dt.Rows)
                   {
                       // while (dr1.Read())
                       foreach (string s in List)
                       {
                           // var arr = s;

                           if (s != "S-1")
                           {

                               if (s == item["locationNo"])
                               {
                                   st.Append("<td style="\"" mode="hold" />                                    st.Append("<td style="\"" mode="hold" />                                    st.Append("<td style="\"" mode="hold" />                                }
                               else
                               {
                                   st.Append("</tr>");
                                   st.Append("<tr>");
                                   st.Append("<td style="\"" mode="hold" />                                    st.Append("<td style="\"" mode="hold" />                                    st.Append("<td style="\"" mode="hold" />
                               }
                           }
                       }
                   st.Append("</tr>");
               }
           }
           dr.Close();
           st.Append("");
           Literal1.Text = st.ToString();

       }

   }
Posted
Updated 12-Mar-15 20:57pm
v4
Comments
Thava Rajan 13-Mar-15 2:51am    
in which line did you get the error
Nishant.Chauhan80 13-Mar-15 3:01am    
while (dr.Read())

1 solution

You don't show us the code you use to set up the reader- but I'm guessing that you use the existing con SqlConnection object to form the connection.
And then in your loop you USSR it again to feed your DatraAdapter, and then:
con.Close();
you close it inside the reader loop, so the reader will be closed as well.

So either don't close it, or create a new connection for your DataAdapter.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900