Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ADO.NET
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 8-Nov-11 19:31pm
Edited 8-Nov-11 20:08pm
hitech_s6.5K
v3
Comments
hitech_ssc at 9-Nov-11 0:39am
   
placed pre tags
SAKryukov at 9-Nov-11 0:54am
   
In what line the exception is thrown?
--SA
hitech_ssc at 9-Nov-11 1:07am
   
if some one is down vote should give explanation i think let me know whats wrong with this..
hitech_ssc at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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;
        }
        
 

    }
 
  Permalink  
v2
Comments
hitech_ssc at 9-Nov-11 1:06am
   
if some one is down vote should give explanation i think
let me know whats wrong with this..
Thendral from chennai at 9-Nov-11 1:18am
   
hi,it was working.thanks for giving this solution
hitech_ssc at 9-Nov-11 1:32am
   
if it is useful plz accept the answer
Sravani.v at 9-Nov-11 1:28am
   
My 5!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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);
            }
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

 

 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();
                }
 

 

 

 

 

 

 

 

 
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 380
1 OriginalGriff 250
2 DamithSL 210
3 Kornfeld Eliyahu Peter 200
4 Peter Leow 95
0 OriginalGriff 7,315
1 DamithSL 5,199
2 Sergey Alexandrovich Kryukov 4,917
3 Maciej Los 4,866
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web04 | 2.8.141223.1 | Last Updated 8 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100