Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
Here is my code to validate the priority if it is already exist.
It was giving the following error,

Error:There is already an open DataReader associated with this Command which must be closed first.

Is there any mistake in the coding?Can anyone explain this?

public int found1(int prior)
{
    if (con.State == ConnectionState.Closed)
    {
        con.Open();
    }
    SqlCommand comm = new SqlCommand("select * from addproduct where priority='" + prior + "'", con);
    SqlDataReader dtr = comm.ExecuteReader();
    if (dtr.HasRows)
    {

        return 1;

    }
    else
    {
        return -1;
    }
    dtr.Close();

}
Posted
Updated 8-Nov-11 19:08pm
v3
Comments
hitech_s 9-Nov-11 0:39am    
placed pre tags
Sergey Alexandrovich Kryukov 9-Nov-11 0:54am    
In what line the exception is thrown?
--SA
hitech_s 9-Nov-11 1:07am    
if some one is down vote should give explanation i think let me know whats wrong with this..
hitech_s 9-Nov-11 1:33am    
this message is for the person who downvoted me..
my solution was useful the author of this question posted that.
may i know who and why downvoted me..

plz dont hurt the people
if there is wrong with the solution tell me i will learn something from that without mentioning anything how can i know

replace your code like this
the reason you are returning before closing the reader thats why error came
public int found1(int prior)
   {
     if (con.State == ConnectionState.Closed)
       {
           con.Open();
       }
       SqlCommand comm = new SqlCommand("select * from addproduct where priority='" + prior + "'", con);
       SqlDataReader dtr = comm.ExecuteReader();
       if (dtr.HasRows)
       {
           dtr.Close();
           return 1;

       }
       else
       {
           dtr.Close();
           return -1;
       }



   }
 
Share this answer
 
v2
Comments
hitech_s 9-Nov-11 1:06am    
if some one is down vote should give explanation i think
let me know whats wrong with this..
Thendral.N 9-Nov-11 1:18am    
hi,it was working.thanks for giving this solution
hitech_s 9-Nov-11 1:32am    
if it is useful plz accept the answer
sravani.v 9-Nov-11 1:28am    
My 5!
string Conn="Database=snpsolution; user id=sa; password=chinu111";
        int Flag = 0;
        SqlConnection con = new SqlConnection("Database=snpsolution; user id=sa; password=chinu111");
        
        //SqlCommandBuilder cb;
        //SqlDataReader dr;
        //DataRow rd;
        //DataSet ds;
        public Form3()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //using (SqlConnection conn = new SqlConnection(Conn))
            //{

            //    using (SqlCommand cmd = new SqlCommand(Quary, conn))
            //    {

            //        //cmd.CommandType = CommandType.StoredProcedure;
            //        con.Open();
            //       id = (int)cmd.ExecuteNonQuery();
            //       con.Close();

            //    }

            //}
            try
            {
                SqlConnection conn = new SqlConnection(Conn);
                conn.Open();



                //For Checking User is Already Registerd.
                string strLoginCheck = "select * from Useraccount";
                SqlCommand cmdLoginCheck = new SqlCommand(strLoginCheck, conn);
                SqlDataReader dr = cmdLoginCheck.ExecuteReader();
                while (dr.Read())
                {
                    if (dr["Userid"].ToString() ==txtusername.Text)
                    {
                        MessageBox.Show("User is Already Exists.", "Snp Solution...    ", MessageBoxButtons.RetryCancel, MessageBoxIcon.Information);
                        Flag = 1;
                    }
                }


                
                if (txtusername. Text.Equals("") || txtPassword. Text.Equals("") || textBox1. Text.Equals(""))
                {
                    MessageBox.Show("Fill the Information.", " Snp Solution...     ", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (txtPassword.Text !=textBox1. Text)
                {
                    MessageBox.Show("Password is Not Match.", "Snp Solution...     ", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (txtPassword. Text ==textBox1. Text && Flag == 0)
                {
                    string str = "insert into Useraccount values('" + txtusername.Text + "','" + txtPassword.Text + "')";
                    SqlCommand cmd = new SqlCommand(str, conn);
                    cmd.ExecuteNonQuery();

                    MessageBox.Show("User Registerd.....     ", "Snp Solution...      ", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                Flag = 0;

                dr.Close();
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Snp Solution...", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
 
Share this answer
 
v2
cmd1 = new SqlCommand("Select subject,july,augest,september,october,halfyearly from studentmark where sgroup='B' AND SERIALNO='" + Wserialno.Text + "'", cl.con);
               SqlDataReader dr1 = cmd1.ExecuteReader();
               // Create a data row of the created table.
               if (dr1.HasRows)
               {
                   while (dr1.Read())
                   {
                       DataRow row = demoTable.NewRow();
                       // Assign values to each cell. We can assign any type of data to
                       // the cell (object type)
                       row["subject"] = dr1[0].ToString();
                       row["july"] = dr1[1].ToString();
                       row["augest"] = dr1[2].ToString();
                       row["september"] = dr1[3].ToString();
                       row["october"] = dr1[4].ToString();
                       row["halfyearly"] = dr1[5].ToString();
                       demoTable.Rows.Add(row);
                   }
                   dr1.Close();
               }
               else
               {
                   dr1.Close();
               }
 
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