Benjamin Allen

Benjamin Allen

  • NA
  • 34
  • 7.8k

Data reader error

Apr 15 2016 8:06 AM

HI

Someone should help me out please. Am having this error"da.fill(ds) there's already an open datareader associated with this command which must be closed first".


Below are my codes. Someone should help me edit it please:


public SqlConnection conn;     public string connString;

    public void connection()     {         connString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ToString();         conn = new SqlConnection(connString);         conn.Open();     }     protected void Page_Load(object sender, EventArgs e)     {             }

    private void bindgrid()     {         SqlCommand cmd = new SqlCommand ("select Day, I, II, III, IV, V, VI from LectureTimeTable where StaffID like '" + TextBox1.Text + "%' or Programme like '" + TextBox1.Text + "%' or Class like '" + TextBox1.Text + "%'", conn);         SqlDataAdapter da = new SqlDataAdapter(cmd);         DataSet ds = new DataSet();         da.Fill(ds);         GridView1.DataSource = ds;         GridView1.DataBind();     }

protected void Button2_Click(object sender, EventArgs e)     {         connection();         string query = "select Day, I, II, III, IV, V, VI from LectureTimeTable where StaffID like '" + TextBox1.Text + "%' or Programme like '" + TextBox1.Text + "%' or Class like '" + TextBox1.Text + "%'";         SqlCommand cmd = new SqlCommand(query, conn);         SqlDataReader dr;         dr = cmd.ExecuteReader();                 if (dr.HasRows)         {             dr.Read();             bindgrid();             GridView1.Visible = true;             TextBox1.Text = "";             Label1.Text = "";         }         else         {             GridView1.Visible = false;             Label1.Visible = true;             Label1.Text = "The Search Term " + TextBox1.Text + "  Is Not Available in the Records"; ;             conn.Close();         }     }

Thanks.

 


Answers (1)