1

I am trying to bind same data reader object to 2 controls .one is gridview and second is formview.The gridview is getting bind but it formview didn't .Even i am closing the object after binding both. Can anyone please let me know if it is possible or not.If yes then how?

This is my code:-

   SqlConnection con = new SqlConnection(getconnectionstring());
   SqlCommand cmd = new SqlCommand();
    SqlCommand cmd1 = new SqlCommand();

   //cmd.CommandText = "SELECT firstname,lastname FROM crudtable";  
    cmd.CommandText = "SELECT firstname,lastname FROM crudtable";
    cmd1.CommandText = "SELECT firstname FROM crudtable";

    cmd.Connection = con;
    cmd1.Connection = con;

    con.Open();

    SqlDataReader reader ;
    SqlDataReader reader1;

    reader = cmd.ExecuteReader();
    GridView1.DataSource = reader;
    GridView1.DataBind();

    FormView1.DataSource = reader;
    FormView1.DataBind();
    reader.Close();
    reader1 = cmd1.ExecuteReader();
    ddl.DataSource = reader1;
    ddl.DataTextField = "firstname";
    ddl.DataBind();

Thanks in advance.

3
  • Please see updated code.The reader object had to close only once after binding it to both controls. Sep 1, 2012 at 13:33
  • Yes of course, but did it work now? Sep 1, 2012 at 13:38
  • i didn't get exception.it is working fine.Grid has its data but form view has nothing.(because it couldn't be bind). Sep 1, 2012 at 13:50

4 Answers 4

4

SqlDataReader is a forward only reader.

Provides a way of reading a forward-only stream of rows from a SQL Server database.

Once the data is read, you can't go back to read it again. That is why you can't use the same reader as a data source for another control without calling ExecuteReader again.

If the number of rows you get is small, you can fetch the data into a DataSet and bind that to both.

SqlConnection conn = new SqlConnection(ConnectionString);
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT firstname,lastname FROM crudtable";
da.SelectCommand = cmd;
DataSet ds = new DataSet();
conn.Open();
da.Fill(ds);
conn.Close();

GridView1.DataSource = ds;
GridView1.DataBind();

FormView1.DataSource = ds;
FormView1.DataBind();
ddl.DataSource = ds;
ddl.DataTextField = "firstname";
ddl.DataBind();

Once you have the data in your DataSet, you can decide which columns to Bind and show.

1
  • it didn't bind formview and dd1. Sep 3, 2012 at 5:47
3

For the most streamlined version of binding in this case I would modify the code like so:

DataTable results = new DataTable();
using (SqlConnection connection = new SqlConnection(getconnectionstring()))
{
    connection.Open();
    using (SqlCommand command = new SqlCommand("SELECT firstname,lastname FROM crudtable",connection))
    {
        results.Load(command.ExecuteReader());
    }
}

GridView1.DataSource = results;
GridView1.DataBind();

FormView1.DataSource = results;
FormView1.DataBind();

ddl.DataSource = results;
ddl.DataTextField = "firstname";
ddl.DataBind();
1

If 2 controls are not binding to 1 data source then take that data source and bind it to table then copy that table And its data to another table and bind them seperatley.

DataTable.clone() will fetch the structure. DatTable.Copy() will fetch schema and records

Clone reference

Copy Reference

0

Finally i took a new reader object to bind formview,but still it didn't work.Am i wrong somewhere or missing something. Getting no exception no error. This is updated code.

    SqlConnection con = new SqlConnection(getconnectionstring());
    SqlCommand cmd = new SqlCommand();
    SqlCommand cmd1 = new SqlCommand();
    SqlCommand cmd2 = new SqlCommand();

   //cmd.CommandText = "SELECT firstname,lastname FROM crudtable";  
    cmd.CommandText = "SELECT firstname,lastname FROM crudtable";
    cmd2.CommandText = "SELECT firstname,lastname FROM crudtable";
    cmd1.CommandText = "SELECT firstname FROM crudtable";

    cmd.Connection = con;
    cmd1.Connection = con;
    cmd2.Connection=con;
    con.Open();

    SqlDataReader reader ;
    SqlDataReader reader1;
    SqlDataReader reader2;
    reader = cmd.ExecuteReader();


    GridView1.DataSource = reader;
    GridView1.DataBind();
    reader.Close();
    //reader.Close();
    reader2 = cmd2.ExecuteReader();
    FormView1.DataSource = reader2;
    FormView1.DataBind();
    reader2.Close();
    reader1 = cmd1.ExecuteReader();
    ddl.DataSource = reader1;
    ddl.DataTextField = "firstname";
    ddl.DataBind();
    reader1.Close();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.