Click here to Skip to main content
16,015,973 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all....!!!

i am checking values that exist in database through datareader and if the value exists then i am binding some other values from that table to datagrid.

The problem is that when it finds the value in database and enters if condition it says datareader already open and if i close the datareader then condition will be checked only once for the first time and then the connection will be terminated.






con.Open();
        SqlCommand cmd = new SqlCommand("Select Productcode, Status from Product", con);
        dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            string code = dr.GetValue(0).ToString();
            string status = dr.GetValue(1).ToString();
            if (status == "Active")
            {
               // dr.Close();
                SqlCommand cmd1 = new SqlCommand("Select Productcode, Name, MRP from Product where Productcode='" + code + "'", con);
                da = new SqlDataAdapter(cmd1);
                ds = new DataSet();
                da.Fill(ds);
                grid_pins.DataSource = ds;
                grid_pins.DataBind();
            }
        }
Posted

Multiple issues with your code.

1) You can not execute SQLCommand when your DataReader is in open mode.

2) I guess you want records from Product Table which are having Status matching with 'Active'.

3) Try by changing your code as below.
C#
//Declaration and initialization of con here.

con.Open();

SqlCommand cmd1 = new SqlCommand("Select Productcode, Name, MRP from Product where Status='Active'", con);

cmd1.CommandType = CommandType.Text;

  da = new SqlDataAdapter(cmd1);
  ds = new DataSet();
  da.Fill(ds);
  grid_pins.DataSource = ds;
  grid_pins.DataBind();
 
Share this answer
 
Why u use two queries..Use Single Query..

C#
SqlConnection con = new SqlConnection(con);
 SqlCommand cmd = new SqlCommand("Select Productcode, Name, MRP from Product where Status='Active'", con);
 SqlDataAdapter da = new SqlDataAdapter(cmd);
 DataSet ds = new DataSet();
 da.Fill(ds);
 grid_pins.DataSource = ds;
 grid_pins.DataBind();
 
Share this answer
 
v2
insted of using sql reader for

SqlCommand cmd = new SqlCommand("Select Productcode, Status from Product", con);

use DataTable and close you connection using

cmd.Connection.close()

store result in datatable before closing Connection and use this datatable

foreach(DataRow dr in datatable.Rows)
{
// your 2nd query
// i.e. SqlCommand cmd1 = new SqlCommand("Select Productcode, Name, MRP from Product where Status='Active'", con);

}
 
Share this answer
 
Hi,


I think, not sure, but you can use
MultipleActiveResultSets=True
in your connection string.that will solve your problem, that is used for more than one active result set.
 
Share this answer
 
v2

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