5
Answers

problem with sql datareader

Hi every body......,
          i got an error as " There is already an open DataReader associated with this Command which must be closed first." while executing the following code.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace Telstra
{
    public partial class Form2 : Form
    {
        SqlConnection con = new SqlConnection("data source=system;user id=sa;password=chowdary;Initial Catalog=usermanagement");
        //string a,b;
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
          
            String st = "select token_no,software_1,software_2 from tokendetails where temp_user_id='" + Class1 .b  + "'";
            String st1 = "select token_no,software_1,software_2 from tokendetails where Shift1_emp='" + Class1 .b  + "'";
            String st2 = "select token_no,software_1,software_2 from tokendetails where Shift2_emp='" + Class1 .b  + "'";
            String st3 = "select DmtNo,DmtName from UserDetails where user_id=" + Class1 .d  + "";
            SqlCommand cmd = new SqlCommand(st, con);
            SqlCommand cmd1 = new SqlCommand(st1, con);
            SqlCommand cmd2 = new SqlCommand(st2, con);
            SqlCommand cmd3 = new SqlCommand(st3, con);
            con.Open();

            SqlDataReader dr = cmd.ExecuteReader();
            SqlDataReader dr1 = cmd1.ExecuteReader();
            SqlDataReader dr2 = cmd2.ExecuteReader();
            SqlDataReader dr3 = cmd3.ExecuteReader();
            if (dr.Read())
            {
                dr3.Read();
                MessageBox.Show(" Temperory these details are available for You ");
                textBox1.Text = dr.GetValue(0).ToString();
                textBox2.Text = dr.GetValue(1).ToString();
                textBox3.Text = dr.GetValue(2).ToString();
                textBox4.Text = dr3.GetValue(0).ToString();
                textBox5.Text = dr3.GetValue(1).ToString();
                con.Close();
            }
            else if (dr1.Read())
            {
                dr3.Read();
                textBox1.Text = dr1.GetValue(0).ToString();
                textBox2.Text = dr1.GetValue(1).ToString();
                textBox3.Text = dr1.GetValue(2).ToString();
                textBox4.Text = dr3.GetValue(0).ToString();
                textBox5.Text = dr3.GetValue(1).ToString();
                con.Close();
            }
            else if (dr2.Read())
            {
                dr3.Read();
                textBox1.Text = dr2.GetValue(0).ToString();
                textBox2.Text = dr2.GetValue(1).ToString();
                textBox3.Text = dr2.GetValue(2).ToString();
                textBox4.Text = dr3.GetValue(0).ToString();
                textBox5.Text = dr3.GetValue(1).ToString();
                con.Close();
            }

          
           
        }
    }
}


please sugget me how to modify the above code to errorless.


Thanks in advance

Answers (5)