473,563 Members | 2,635 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Binding a Datareader to Dropdown

I need some help on binding a datareader to a dropdown box. I have included
the code for the dropdown below. It builds with no errors, but returns no
results. Any help would be appreciated.

private void DropDownList1_S electedIndexCha nged(object sender,
System.EventArg s e)

{

OleDbConnection conn = new OleDbConnection ("DataSource=nt drp001.world;
integrated security =true;"+ "initial catalog = Request");

conn.Open();

OleDbCommand cmd = new OleDbCommand("S ELECT distinct(cuid) from Request",
conn);
OleDbDataReader dreader = cmd.ExecuteRead er();

DropDownList1.D ataSource = dreader;

DropDownList1.D ataValueField = "cuid";

DropDownList1.D ataTextField = "cuid";

DropDownList1.S electedIndex = 0;

DropDownList1.D ataBind();

dreader.Close() ;

conn.Close();

}
Nov 18 '05 #1
4 1949
First off you need to add the Databind code to the Page_Load section of the
code, not the SelectedIndex Changed section.

Also, the biggest mistake people make when binding data to a dropdown is
that they rebind the data on every Page_Load. So be sure that in your
page_load section of the code you wrap the binding inside an if
(!Page.IsPostBa ck) block.

So it should look like this:
private void DropDownList1_S electedIndexCha nged(object sender,
System.EventArg s e)
{
// Do something with the changed value
}

private void Page_Load(objec t sender, System.EventArg s e)
{
if ( !Page.IsPostBac k )
{
OleDbConnection conn = new
OleDbConnection ("DataSource=nt drp001.world;
integrated security =true;"+ "initial catalog = Request");
conn.Open();
OleDbCommand cmd = new OleDbCommand("S ELECT distinct(cuid) from
Request",
conn);

OleDbDataReader dreader = cmd.ExecuteRead er();
DropDownList1.D ataSource = dreader;
DropDownList1.D ataValueField = "cuid";
DropDownList1.D ataTextField = "cuid";
DropDownList1.S electedIndex = 0;
DropDownList1.D ataBind();
dreader.Close() ;
conn.Close();
}
}

"Brian Conway" <Br**********@q west.com> wrote in message
news:ek******** *****@tk2msftng p13.phx.gbl...
I need some help on binding a datareader to a dropdown box. I have included the code for the dropdown below. It builds with no errors, but returns no
results. Any help would be appreciated.

private void DropDownList1_S electedIndexCha nged(object sender,
System.EventArg s e)

{

OleDbConnection conn = new OleDbConnection ("DataSource=nt drp001.world;
integrated security =true;"+ "initial catalog = Request");

conn.Open();

OleDbCommand cmd = new OleDbCommand("S ELECT distinct(cuid) from Request",
conn);
OleDbDataReader dreader = cmd.ExecuteRead er();

DropDownList1.D ataSource = dreader;

DropDownList1.D ataValueField = "cuid";

DropDownList1.D ataTextField = "cuid";

DropDownList1.S electedIndex = 0;

DropDownList1.D ataBind();

dreader.Close() ;

conn.Close();

}

Nov 18 '05 #2
You put the code in the SelectedIndexCh anged event of the dropdown. That
means you first have to select something from the dropdown, then you need to
go back to the server, and only then will this code run.

Are you sure this is the event you want to place this code in?

"Brian Conway" <Br**********@q west.com> wrote in message
news:ek******** *****@tk2msftng p13.phx.gbl...
I need some help on binding a datareader to a dropdown box. I have included the code for the dropdown below. It builds with no errors, but returns no
results. Any help would be appreciated.

private void DropDownList1_S electedIndexCha nged(object sender,
System.EventArg s e)

{

OleDbConnection conn = new OleDbConnection ("DataSource=nt drp001.world;
integrated security =true;"+ "initial catalog = Request");

conn.Open();

OleDbCommand cmd = new OleDbCommand("S ELECT distinct(cuid) from Request",
conn);
OleDbDataReader dreader = cmd.ExecuteRead er();

DropDownList1.D ataSource = dreader;

DropDownList1.D ataValueField = "cuid";

DropDownList1.D ataTextField = "cuid";

DropDownList1.S electedIndex = 0;

DropDownList1.D ataBind();

dreader.Close() ;

conn.Close();

}

Nov 18 '05 #3
Ok now I am getting the following error upon moving the code.

Server Error in '/Conference Request' Application.
----------------------------------------------------------------------------
----

No error information available: REGDB_E_CLASSNO TREG(0x80040154 ).
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.Data.Ole Db.OleDbExcepti on: No error information
available: REGDB_E_CLASSNO TREG(0x80040154 ).

Source Error:

Line 62: {
Line 63: OleDbConnection conn = new
OleDbConnection ("Provider=ntdr p001.world; integrated security =true;"+
"initial catalog = Request");
Line 64: conn.Open();
Line 65: OleDbCommand cmd = new OleDbCommand("S ELECT distinct(cuid) from
Request", conn);
Line

"Ben Dewey" <be*******@scie ntiae.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
First off you need to add the Databind code to the Page_Load section of the code, not the SelectedIndex Changed section.

Also, the biggest mistake people make when binding data to a dropdown is
that they rebind the data on every Page_Load. So be sure that in your
page_load section of the code you wrap the binding inside an if
(!Page.IsPostBa ck) block.

So it should look like this:
private void DropDownList1_S electedIndexCha nged(object sender,
System.EventArg s e)
{
// Do something with the changed value
}

private void Page_Load(objec t sender, System.EventArg s e)
{
if ( !Page.IsPostBac k )
{
OleDbConnection conn = new
OleDbConnection ("DataSource=nt drp001.world;
integrated security =true;"+ "initial catalog = Request");
conn.Open();
OleDbCommand cmd = new OleDbCommand("S ELECT distinct(cuid) from Request",
conn);

OleDbDataReader dreader = cmd.ExecuteRead er();
DropDownList1.D ataSource = dreader;
DropDownList1.D ataValueField = "cuid";
DropDownList1.D ataTextField = "cuid";
DropDownList1.S electedIndex = 0;
DropDownList1.D ataBind();
dreader.Close() ;
conn.Close();
}
}

"Brian Conway" <Br**********@q west.com> wrote in message
news:ek******** *****@tk2msftng p13.phx.gbl...
I need some help on binding a datareader to a dropdown box. I have

included
the code for the dropdown below. It builds with no errors, but returns no results. Any help would be appreciated.

private void DropDownList1_S electedIndexCha nged(object sender,
System.EventArg s e)

{

OleDbConnection conn = new OleDbConnection ("DataSource=nt drp001.world;
integrated security =true;"+ "initial catalog = Request");

conn.Open();

OleDbCommand cmd = new OleDbCommand("S ELECT distinct(cuid) from Request", conn);
OleDbDataReader dreader = cmd.ExecuteRead er();

DropDownList1.D ataSource = dreader;

DropDownList1.D ataValueField = "cuid";

DropDownList1.D ataTextField = "cuid";

DropDownList1.S electedIndex = 0;

DropDownList1.D ataBind();

dreader.Close() ;

conn.Close();

}


Nov 18 '05 #4
You didn't say which line is causing it, I'm guess the connection cannot be
opened. More then likely, it is the integrated security part. You probably
don't have the proper permissions set up for the ASPNET user to be able to
connect to the database.

"Brian Conway" <Br**********@q west.com> wrote in message
news:ub******** ******@TK2MSFTN GP11.phx.gbl...
Ok now I am getting the following error upon moving the code.

Server Error in '/Conference Request' Application.
-------------------------------------------------------------------------- -- ----

No error information available: REGDB_E_CLASSNO TREG(0x80040154 ).
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.Data.Ole Db.OleDbExcepti on: No error information
available: REGDB_E_CLASSNO TREG(0x80040154 ).

Source Error:

Line 62: {
Line 63: OleDbConnection conn = new
OleDbConnection ("Provider=ntdr p001.world; integrated security =true;"+
"initial catalog = Request");
Line 64: conn.Open();
Line 65: OleDbCommand cmd = new OleDbCommand("S ELECT distinct(cuid) from
Request", conn);
Line

"Ben Dewey" <be*******@scie ntiae.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
First off you need to add the Databind code to the Page_Load section of the
code, not the SelectedIndex Changed section.

Also, the biggest mistake people make when binding data to a dropdown is
that they rebind the data on every Page_Load. So be sure that in your
page_load section of the code you wrap the binding inside an if
(!Page.IsPostBa ck) block.

So it should look like this:
private void DropDownList1_S electedIndexCha nged(object sender,
System.EventArg s e)
{
// Do something with the changed value
}

private void Page_Load(objec t sender, System.EventArg s e)
{
if ( !Page.IsPostBac k )
{
OleDbConnection conn = new
OleDbConnection ("DataSource=nt drp001.world;
integrated security =true;"+ "initial catalog = Request");
conn.Open();
OleDbCommand cmd = new OleDbCommand("S ELECT distinct(cuid)

from
Request",
conn);

OleDbDataReader dreader = cmd.ExecuteRead er();
DropDownList1.D ataSource = dreader;
DropDownList1.D ataValueField = "cuid";
DropDownList1.D ataTextField = "cuid";
DropDownList1.S electedIndex = 0;
DropDownList1.D ataBind();
dreader.Close() ;
conn.Close();
}
}

"Brian Conway" <Br**********@q west.com> wrote in message
news:ek******** *****@tk2msftng p13.phx.gbl...
I need some help on binding a datareader to a dropdown box. I have

included
the code for the dropdown below. It builds with no errors, but
returns no results. Any help would be appreciated.

private void DropDownList1_S electedIndexCha nged(object sender,
System.EventArg s e)

{

OleDbConnection conn = new OleDbConnection ("DataSource=nt drp001.world;
integrated security =true;"+ "initial catalog = Request");

conn.Open();

OleDbCommand cmd = new OleDbCommand("S ELECT distinct(cuid) from Request", conn);
OleDbDataReader dreader = cmd.ExecuteRead er();

DropDownList1.D ataSource = dreader;

DropDownList1.D ataValueField = "cuid";

DropDownList1.D ataTextField = "cuid";

DropDownList1.S electedIndex = 0;

DropDownList1.D ataBind();

dreader.Close() ;

conn.Close();

}



Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
5751
by: Jason Huang | last post by:
Hi, Is it possible to bind DataReader to a DataGrid in C# windows form? And how? And can we update data in a DataSet by using the DataReader? Thanks for help. Jason
2
8188
by: Jake S | last post by:
Hi all, Is it possibele to set the datasource of a dropdown list to a datareader? When I try to the only column I receive is a column populated by the datasource name repeated the amount of times equivilant to the number of records returned, instead of the two columns(value and text) I have populated the datareader with. Any ideas?
1
1824
by: Sachin Kuchinad | last post by:
Hi All I am using .NET Framework version 1.0.3705 to build a website. Now i have made a webform and on that i have added a dropdown to the page I bind this listbox to a datareader . This does not happenm , i have no clue what the problem is I am attaching the error <font color='red' >
1
5306
by: Sachin Kuchinad | last post by:
Hi All I am using .NET Framework version 1.0.3705 to build a website. Now i have made a webform and on that i have added a dropdown to the page I bind this listbox to a datareader . This does not happenm , i have no clue what the problem is I am attaching the error <font color='red' >
2
1401
by: Darrel | last post by:
I'm trying to perform a SQL query and then bind the results to two different dropdownlists. Unfortunately, I can only get it to bind to one...whichever one of the two I try to bind first: Dim objConnect As New OleDb.OleDbConnection(strConnect) objConnect.Open() Dim objCommand As New System.Data.OleDb.OleDbCommand(strSQL, objConnect)...
5
4212
by: Vigneshwar Pilli via DotNetMonster.com | last post by:
string connectionString1 = "server=(local); user=sa;password=sa; database=sonic"; System.Data.SqlClient.SqlConnection dbConnection1 = new System.Data.SqlClient.SqlConnection(connectionString1); System.Data.SqlClient.SqlCommand dbCommand1 = new System.Data.SqlClient.SqlCommand();
3
1897
by: tshad | last post by:
Is there a way to databind a datareader to 2 different dropdownlists? Something like: StoredSearches.DataSource=objCmd.ExecuteReader StoredSearches.DataValueField="SearchID" StoredSearches.DataTextField= "SearchName" StoredSearches.databind() SearchList.DataSource=objCmd.ExecuteReader SearchList.DataValueField="SearchID"...
3
2486
by: Carlos Lozano | last post by:
Hello, I am having a problem getting the selectedValue from a dropdownlist that is populated with a dataReader and just can't see the problem. I did the following: dim dr as DataReader dr = DataReader(sSQLcmd) Me.DropDownList1.DataSource = dr
1
1759
by: js | last post by:
I am using executeReader to get a SqlDataReader as in my code below. The SqlDataReader contains two tables. I am trying to bind each table to its respective dropdown list control. Thanks. //************* My code ************* System.Data.SqlClient.SqlDataReader dr = null; string strSQL = "SELECT id, iorg_name FROM int_org order by...
0
7659
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7580
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7882
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7634
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6244
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5208
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3618
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1194
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.