Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

I got this error.how can i resolve this?
is there any mistake in tat?

C#
public void root()
    {
        string user_sig, sig, categ, pkey;
        con.Open();
        SqlCommand cmd = new SqlCommand("select user_signature,signature,category,private_key from signature", con);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows)
        {
            while (dr.Read())
            {
                user_sig = dr["user_signature"].ToString();
                sig = dr["signature"].ToString();
                categ = dr["category"].ToString();
                string result = "";
                string sig1 = categ;
                string num = sig1.Remove(1);
                Label1.Text = num;
                //Label2.Text = categ;
                switch(categ)
                {
                    case "11":
                       
                        if (con.State == ConnectionState.Closed)
                        {
                            con.Open();
                        }
                        dr.Close();
                        SqlCommand cmd1 = new SqlCommand("select distinct category from signature", con);
                        SqlDataReader dr1 = cmd1.ExecuteReader();
                        if (dr1.HasRows)
                        {
                            while (dr1.Read())
                            {
                                dr.Close();
                                string categ_sig = dr1["category"].ToString();
                                Label lblcateg = new Label();
                                lblcateg.Text = categ_sig;
                                this.Controls.Add(lblcateg);
                            }
                            dr1.Close();
                        }
                        dr1.Close();
                        con.Close();
                        break;
                
                    case "12":
                    Label lblcateg1 = new Label();
                    lblcateg1.Text = categ;
                    this.Controls.Add(lblcateg1);
                    break;
                    //dr1.Close();
                }
                
            }
            //dr.Close();
        }
Posted
Updated 23-Feb-12 2:19am
Comments
ZurdoDev 23-Feb-12 8:10am    
Just debug it and see what line is causing it.
Thendral.N 23-Feb-12 8:13am    
i got error from first while loop tat is while(dr.read())

1 solution

Don't call Ddldr.Close();, especially inside the while. This way you are doing a first iteration, closing the reader and the second iteration will of course booom as the reader is closed.
 
Share this answer
 
Comments
Thendral.N 23-Feb-12 8:27am    
its working fine.

thanks
Syed Salman Raza Zaidi 23-Feb-12 8:33am    
accept it as 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