Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Binding a Datareader to Dropdown

Reply
Thread Tools

Binding a Datareader to Dropdown

 
 
Brian Conway
Guest
Posts: n/a
 
      05-11-2004
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_SelectedIndexChanged(object sender,
System.EventArgs e)

{

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

conn.Open();

OleDbCommand cmd = new OleDbCommand("SELECT distinct(cuid) from Request",
conn);


OleDbDataReader dreader = cmd.ExecuteReader();

DropDownList1.DataSource = dreader;

DropDownList1.DataValueField = "cuid";

DropDownList1.DataTextField = "cuid";

DropDownList1.SelectedIndex = 0;

DropDownList1.DataBind();

dreader.Close();

conn.Close();

}


 
Reply With Quote
 
 
 
 
Ben Dewey
Guest
Posts: n/a
 
      05-11-2004
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.IsPostBack) block.

So it should look like this:
private void DropDownList1_SelectedIndexChanged(object sender,
System.EventArgs e)
{
// Do something with the changed value
}

private void Page_Load(object sender, System.EventArgs e)
{
if ( !Page.IsPostBack )
{
OleDbConnection conn = new
OleDbConnection("DataSource=ntdrp001.world;
integrated security =true;"+ "initial catalog = Request");
conn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT distinct(cuid) from
Request",
conn);

OleDbDataReader dreader = cmd.ExecuteReader();
DropDownList1.DataSource = dreader;
DropDownList1.DataValueField = "cuid";
DropDownList1.DataTextField = "cuid";
DropDownList1.SelectedIndex = 0;
DropDownList1.DataBind();
dreader.Close();
conn.Close();
}
}

"Brian Conway" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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_SelectedIndexChanged(object sender,
> System.EventArgs e)
>
> {
>
> OleDbConnection conn = new OleDbConnection("DataSource=ntdrp001.world;
> integrated security =true;"+ "initial catalog = Request");
>
> conn.Open();
>
> OleDbCommand cmd = new OleDbCommand("SELECT distinct(cuid) from Request",
> conn);
>
>
> OleDbDataReader dreader = cmd.ExecuteReader();
>
> DropDownList1.DataSource = dreader;
>
> DropDownList1.DataValueField = "cuid";
>
> DropDownList1.DataTextField = "cuid";
>
> DropDownList1.SelectedIndex = 0;
>
> DropDownList1.DataBind();
>
> dreader.Close();
>
> conn.Close();
>
> }
>
>



 
Reply With Quote
 
 
 
 
Marina
Guest
Posts: n/a
 
      05-11-2004
You put the code in the SelectedIndexChanged 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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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_SelectedIndexChanged(object sender,
> System.EventArgs e)
>
> {
>
> OleDbConnection conn = new OleDbConnection("DataSource=ntdrp001.world;
> integrated security =true;"+ "initial catalog = Request");
>
> conn.Open();
>
> OleDbCommand cmd = new OleDbCommand("SELECT distinct(cuid) from Request",
> conn);
>
>
> OleDbDataReader dreader = cmd.ExecuteReader();
>
> DropDownList1.DataSource = dreader;
>
> DropDownList1.DataValueField = "cuid";
>
> DropDownList1.DataTextField = "cuid";
>
> DropDownList1.SelectedIndex = 0;
>
> DropDownList1.DataBind();
>
> dreader.Close();
>
> conn.Close();
>
> }
>
>



 
Reply With Quote
 
Brian Conway
Guest
Posts: n/a
 
      05-11-2004
Ok now I am getting the following error upon moving the code.

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

No error information available: REGDB_E_CLASSNOTREG(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.OleDb.OleDbException: No error information
available: REGDB_E_CLASSNOTREG(0x80040154).

Source Error:

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





"Ben Dewey" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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.IsPostBack) block.
>
> So it should look like this:
> private void DropDownList1_SelectedIndexChanged(object sender,
> System.EventArgs e)
> {
> // Do something with the changed value
> }
>
> private void Page_Load(object sender, System.EventArgs e)
> {
> if ( !Page.IsPostBack )
> {
> OleDbConnection conn = new
> OleDbConnection("DataSource=ntdrp001.world;
> integrated security =true;"+ "initial catalog = Request");
> conn.Open();
> OleDbCommand cmd = new OleDbCommand("SELECT distinct(cuid)

from
> Request",
> conn);
>
> OleDbDataReader dreader = cmd.ExecuteReader();
> DropDownList1.DataSource = dreader;
> DropDownList1.DataValueField = "cuid";
> DropDownList1.DataTextField = "cuid";
> DropDownList1.SelectedIndex = 0;
> DropDownList1.DataBind();
> dreader.Close();
> conn.Close();
> }
> }
>
> "Brian Conway" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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_SelectedIndexChanged(object sender,
> > System.EventArgs e)
> >
> > {
> >
> > OleDbConnection conn = new OleDbConnection("DataSource=ntdrp001.world;
> > integrated security =true;"+ "initial catalog = Request");
> >
> > conn.Open();
> >
> > OleDbCommand cmd = new OleDbCommand("SELECT distinct(cuid) from

Request",
> > conn);
> >
> >
> > OleDbDataReader dreader = cmd.ExecuteReader();
> >
> > DropDownList1.DataSource = dreader;
> >
> > DropDownList1.DataValueField = "cuid";
> >
> > DropDownList1.DataTextField = "cuid";
> >
> > DropDownList1.SelectedIndex = 0;
> >
> > DropDownList1.DataBind();
> >
> > dreader.Close();
> >
> > conn.Close();
> >
> > }
> >
> >

>
>



 
Reply With Quote
 
Marina
Guest
Posts: n/a
 
      05-11-2004
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" <(E-Mail Removed)> wrote in message
news:ub$(E-Mail Removed)...
> Ok now I am getting the following error upon moving the code.
>
> Server Error in '/Conference Request' Application.
> --------------------------------------------------------------------------

--
> ----
>
> No error information available: REGDB_E_CLASSNOTREG(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.OleDb.OleDbException: No error information
> available: REGDB_E_CLASSNOTREG(0x80040154).
>
> Source Error:
>
> Line 62: {
> Line 63: OleDbConnection conn = new
> OleDbConnection("Provider=ntdrp001.world; integrated security =true;"+
> "initial catalog = Request");
> Line 64: conn.Open();
> Line 65: OleDbCommand cmd = new OleDbCommand("SELECT distinct(cuid) from
> Request", conn);
> Line
>
>
>
>
>
> "Ben Dewey" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> > 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.IsPostBack) block.
> >
> > So it should look like this:
> > private void DropDownList1_SelectedIndexChanged(object sender,
> > System.EventArgs e)
> > {
> > // Do something with the changed value
> > }
> >
> > private void Page_Load(object sender, System.EventArgs e)
> > {
> > if ( !Page.IsPostBack )
> > {
> > OleDbConnection conn = new
> > OleDbConnection("DataSource=ntdrp001.world;
> > integrated security =true;"+ "initial catalog = Request");
> > conn.Open();
> > OleDbCommand cmd = new OleDbCommand("SELECT distinct(cuid)

> from
> > Request",
> > conn);
> >
> > OleDbDataReader dreader = cmd.ExecuteReader();
> > DropDownList1.DataSource = dreader;
> > DropDownList1.DataValueField = "cuid";
> > DropDownList1.DataTextField = "cuid";
> > DropDownList1.SelectedIndex = 0;
> > DropDownList1.DataBind();
> > dreader.Close();
> > conn.Close();
> > }
> > }
> >
> > "Brian Conway" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > 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_SelectedIndexChanged(object sender,
> > > System.EventArgs e)
> > >
> > > {
> > >
> > > OleDbConnection conn = new OleDbConnection("DataSource=ntdrp001.world;
> > > integrated security =true;"+ "initial catalog = Request");
> > >
> > > conn.Open();
> > >
> > > OleDbCommand cmd = new OleDbCommand("SELECT distinct(cuid) from

> Request",
> > > conn);
> > >
> > >
> > > OleDbDataReader dreader = cmd.ExecuteReader();
> > >
> > > DropDownList1.DataSource = dreader;
> > >
> > > DropDownList1.DataValueField = "cuid";
> > >
> > > DropDownList1.DataTextField = "cuid";
> > >
> > > DropDownList1.SelectedIndex = 0;
> > >
> > > DropDownList1.DataBind();
> > >
> > > dreader.Close();
> > >
> > > conn.Close();
> > >
> > > }
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP.NET binding to DataReader Andrew Robinson ASP .Net 8 08-28-2007 04:08 AM
binding multi-table datareader js ASP .Net 1 09-22-2006 11:59 AM
Loop through datareader after binding to repeater control sck10 ASP .Net 2 09-12-2006 07:24 PM
bind a dropdown in a column in a datagrid based on the dropdown value selected in another column of the datagrid. vishnu ASP .Net 1 03-25-2006 01:24 PM
Issue With Binding Of A DataReader to a Drop-Down or List Box Cont =?Utf-8?B?U2FjaGluIEt1Y2hpbmFk?= ASP .Net 1 07-22-2004 12:00 PM



Advertisments