Binding a Datareader to Dropdown

Discussion in 'ASP .Net' started by Brian Conway, May 11, 2004.

  1. Brian Conway

    Brian Conway Guest

    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();

    }
     
    Brian Conway, May 11, 2004
    #1
    1. Advertising

  2. Brian Conway

    Ben Dewey Guest

    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" <> wrote in message
    news:...
    > 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();
    >
    > }
    >
    >
     
    Ben Dewey, May 11, 2004
    #2
    1. Advertising

  3. Brian Conway

    Marina Guest

    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" <> wrote in message
    news:...
    > 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();
    >
    > }
    >
    >
     
    Marina, May 11, 2004
    #3
  4. Brian Conway

    Brian Conway Guest

    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" <> wrote in message
    news:%...
    > 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" <> wrote in message
    > news:...
    > > 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();
    > >
    > > }
    > >
    > >

    >
    >
     
    Brian Conway, May 11, 2004
    #4
  5. Brian Conway

    Marina Guest

    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" <> wrote in message
    news:ub$...
    > 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" <> wrote in message
    > news:%...
    > > 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" <> wrote in message
    > > news:...
    > > > 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();
    > > >
    > > > }
    > > >
    > > >

    > >
    > >

    >
    >
     
    Marina, May 11, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U2FjaGluIEt1Y2hpbmFk?=

    Issue With Binding Of A DataReader to a Drop-Down or List Box Cont

    =?Utf-8?B?U2FjaGluIEt1Y2hpbmFk?=, Jul 22, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    3,157
    =?Utf-8?B?U2FjaGluIEt1Y2hpbmFk?=
    Jul 22, 2004
  2. vishnu
    Replies:
    1
    Views:
    1,214
    Patrick.O.Ige
    Mar 25, 2006
  3. sck10
    Replies:
    2
    Views:
    5,462
    sck10
    Sep 12, 2006
  4. js
    Replies:
    1
    Views:
    422
    Mohamed Mosalem
    Sep 22, 2006
  5. Andrew Robinson

    ASP.NET binding to DataReader

    Andrew Robinson, Aug 24, 2007, in forum: ASP .Net
    Replies:
    8
    Views:
    582
    Steven Cheng[MSFT]
    Aug 28, 2007
Loading...

Share This Page