Click here to Skip to main content
15,902,299 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to know that how to bind DropDownList and GridView by one SqlDataReaderobject?
C#
SqlDataReader dr = cmd.ExecuteReader();
              
               GridView1.DataSource = dr;
                SearchDropDownList.DataSource = dr;
                SearchDropDownList.DataValueField = "DesigID";
               GridView1.DataBind();
               SearchDropDownList.DataBind();
               SearchDropDownList.Items.Insert(0, new ListItem("Select Designation ID", "Default value"));
               dr.Close();
and i have tried following
C#
SqlDataReader dr = cmd.ExecuteReader();
            SqlDataReader dr1 = dr;
            GridView1.DataSource = dr;
             SearchDropDownList.DataSource = dr1;
             SearchDropDownList.DataValueField = "DesigID";
            GridView1.DataBind();
            SearchDropDownList.DataBind();
            SearchDropDownList.Items.Insert(0, new ListItem("Select Designation ID", "Default value"));
            dr.Close();

but it is filling only GridView1.
Posted

I would suggest you create a custom web control in your ASP.NET application which would contain both the DropDownList and GridView objects.

In this control you should also place a suitable datasource from the data section of the toolbox such as ObjectDataSource.

Bind both objects to this dataSource, then place the custom control containing all of this in your page.

Thats how I would do it anyway.
 
Share this answer
 
Comments
Member 7909353 14-May-12 8:16am    
Thanks,Your solution is correct but I want to use only sqldatareader.
Why not use DataTable? Fill it with DataAdapter and then use the DataTable with multiple controls as a DataSource.

Have a look at these knowledge-base:
MSDN: DataAdapter Parameters (ADO.NET)[^]
MSDN: Accessing Data with ADO.NET[^]
 
Share this answer
 
Comments
Member 7909353 14-May-12 8:34am    
Thanks,Your solution is correct but I want to use only sqldatareader.
Sandeep Mewara 14-May-12 9:02am    
You cannot do it. DataReader is one way forward only thingy. Once it is used by one datasource, you cannot re-use the same one. Hence, my suggestion to use a datatable.
try like this

C#
SqlDataReader dr ;//declare globally
SqlCommand cmd//declare globally

//In Button Click Write This
 cmd = New SqlCommand("select * from sample", con)

dr= cmd.ExecuteReader();
                
               GridView1.DataSource = dr;
 GridView1.DataBind();
 dr.Close()

      dr=cmd.ExecuteReader();
 SearchDropDownList.DataSource = dr;
SearchDropDownList.DataTextField="DesigID";
                SearchDropDownList.DataValueField = "DesigID";
  SearchDropDownList.DataBind();
              
             
               SearchDropDownList.Items.Insert(0, new ListItem("Select Designation ID", "Default value"));
               dr1.Close();
 
Share this answer
 
v5
Comments
Member 7909353 14-May-12 8:15am    
No this is not working.
You can not use DataReader in this way. Once you read data from it (first DataBind()) there is no way to "rewind" it and read data again.
You could call ExecuteReader() again, but that means new database access.
I suggest you use one of the approaches above (SqlDataSource, ObjectDataSource, DataTable...).
 
Share this answer
 
v2
Ok,
If you absolutely need to use an SQL datareader use it with a datatable like so
VB
Dim dt As New DataTable
Dim dr As New System.Data.SqlClient.SqlDataReader
'TODO Fill datareader here
dt.Load(dr)


Use the datatable as the datasource for your page controls
This should work.

...BUT... its a bad idea to link to your page to your database directly.

Consider using an intermediary object like a business layer object.
 
Share this answer
 

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