Click here to Skip to main content
15,880,503 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
         int st1, st2;
         SqlDataReader rd;
         SqlDataReader rd1;

 private void connection()
        {
            if (cn.State == ConnectionState.Open)
            {
                cn.Close();
            }
            cn.Open();
        }  
private void button2_Click(object sender, EventArgs e)
        {
            connection();

            this.st1 = Convert.ToInt32(txtC.Text);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = cn;
            cmd.CommandText = ("select *  from OrderedFood where coustomerId=@CoustomerId");
            cmd.Parameters.AddWithValue("@CoustomerId", this.st1);
            rd = cmd.ExecuteReader();

           while (rd.Read())
            {
                this.st2 = Convert.ToInt32(rd.GetValue(2).ToString());
                MessageBox.Show(st2.ToString());

                 SqlCommand cmd1 = new SqlCommand();
                cmd1.Connection = cn;
                cmd1.CommandText = ("select [name] from Food where id=@FoodId");
                cmd1.Parameters.AddWithValue("@FoodID", this.st2);
                rd1 = cmd1.ExecuteReader();
                if (rd1.Read())
                {
                    string z;
                    z = rd1.GetValue(0).ToString();
                    for (int i = 0; i <= (checkedListBox1.Items.Count - 1); i++)
                    {
                        checkedListBox1.SetItemChecked(i, false); 
                        string x = checkedListBox1.Items[i].ToString();
                        if (z == x)
                        {
                            checkedListBox1.SetItemChecked(i, true); 
                     

                        }

                    }
                }

            }
         }


Now this shows an error"There is already an open DataReader associated with this Command which must be closed first.".
But if is close data reader i can not be able to get data as while (rd.Read())
reader dose not complete retrive data. So what should i do?
Posted
Updated 9-Sep-11 10:10am
v4
Comments
Herman<T>.Instance 9-Sep-11 6:45am    
I miss the rd1.Close(); in your code.
so when rd goes to the next line in the reader the rd1 is still open.
After while (rd.Reed()) { } don't forget to close rd too!
Pravin Patil, Mumbai 9-Sep-11 6:57am    
100% correct answer....
Md.Arifur Rahman Paragh 9-Sep-11 16:15pm    
Thanks but I tried that then it tells error "Reader already closed". But I found a solution that i made two separate connection for the two reader. then it worked.

you haven't closed yo if statement mate if(Rd1.read()).

This should actually be a while statement while rd1 is reading do this when there is no more data to read stop:


rd1 = cmd1.ExecuteReader();
                while (rd1.Read())
                {
                    string z;
                    z = rd1.GetValue(0).ToString();
                    for (int i = 0; i <= (checkedListBox1.Items.Count - 1); i++)
                    {
                        checkedListBox1.SetItemChecked(i, false); 
                        string x = checkedListBox1.Items[i].ToString();
                        if (z == x)
                        {
                            checkedListBox1.SetItemChecked(i, true); 
                     
 
                        }
                 }


try something like that
 
Share this answer
 
Comments
Herman<T>.Instance 9-Sep-11 7:05am    
good answer
DanHodgson88 9-Sep-11 7:08am    
:) cheers mate
Md.Arifur Rahman Paragh 9-Sep-11 16:16pm    
thanks but that was not i asked for.
I found a solution .I made two separate connection for the two reader. then it worked.
 
Share this answer
 
Comments
CHill60 31-Jul-13 14:12pm    
Why come back nearly 2 years later and post another solution that is the same as the comment you posted in 2011??

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