How to use a single DataReader associated with two different tables to databind two different listbox controls

Platform: ASP.NET| Category: ListBox

VB.NET


Dim cn As SqlConnection
Dim cmd As SqlCommand
Dim dr As SqlDataReader
cn = New SqlConnection('Server=localhost;uid=sa;database=northwind;pwd=')
cmd = New SqlCommand('select * from products;select * from categories', cn)
cn.Open()
dr = cmd.ExecuteReader()
ListBox1.DataSource = dr
ListBox1.DataTextField = 'productname'
ListBox1.DataBind()
dr.NextResult()
ListBox2.DataSource = dr
ListBox2.DataTextField = 'categoryname'
ListBox2.DataBind()

C#


SqlConnection cn;
SqlCommand cmd;
SqlDataReader dr;
cn = new SqlConnection('Server=localhost;uid=sa;database=northwind;pwd=');
cmd= new SqlCommand ('select * from products;select * from categories', cn);
cn.Open();
dr = cmd.ExecuteReader();
ListBox1.DataSource = dr; 
ListBox1.DataTextField = 'productname'; 
ListBox1.DataBind(); 
dr.NextResult();
ListBox2.DataSource = dr; 
ListBox2.DataTextField = 'categoryname'; 
ListBox2.DataBind(); 

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.