DataBind to DropDownList Problem "The IListSource does not contain any data sources"

Discussion in 'ASP .Net' started by Assimalyst, Jul 8, 2005.

  1. Assimalyst

    Assimalyst Guest

    Hi,

    I'm attempting to use a data reader to load data from a single table,
    tblCountry, with two columns, countryNo (the Key) and countryName, into
    a DropDownList box. Here's the code:


    protected System.Data.SqlClient.SqlConnection conn;
    protected System.Data.SqlClient.SqlDataReader drCountry;

    private void Page_Load(object sender, System.EventArgs e)
    {
    string SQL = "SELECT * FROM tblCountry";

    SqlConnection conn = new
    SqlConnection("Server=(local);Database=YLCdbSQL;Integrated
    Security=SSPI");
    SqlCommand countryLoad = new SqlCommand (SQL, conn);
    SqlDataReader drCountry = null;

    try
    {
    conn.Open();
    drCountry = countryLoad.ExecuteReader();

    countryDropLst.DataSource = drCountry;
    countryDropLst.DataMember = "countryName";
    countryDropLst.DataTextField = "countryName";
    countryDropLst.DataValueField = "countryNo";
    countryDropLst.DataBind();
    }
    finally
    {
    drCountry.Close();
    conn.Close();
    }
    }



    But when I run it I get the error "The IListSource does not contain any
    data sources", and the DataBind line is highlighted.

    I have checked the table using Enterprise Manager, it exists and has
    numerous entries in both columns.

    Any idea what the problem might be?

    Thanks.
     
    Assimalyst, Jul 8, 2005
    #1
    1. Advertising

  2. RE: DataBind to DropDownList Problem "The IListSource does not contain

    you set your datareader to null... should be countryLoad.ExecuteReader()

    "Assimalyst" wrote:

    > Hi,
    >
    > I'm attempting to use a data reader to load data from a single table,
    > tblCountry, with two columns, countryNo (the Key) and countryName, into
    > a DropDownList box. Here's the code:
    >
    >
    > protected System.Data.SqlClient.SqlConnection conn;
    > protected System.Data.SqlClient.SqlDataReader drCountry;
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > string SQL = "SELECT * FROM tblCountry";
    >
    > SqlConnection conn = new
    > SqlConnection("Server=(local);Database=YLCdbSQL;Integrated
    > Security=SSPI");
    > SqlCommand countryLoad = new SqlCommand (SQL, conn);
    > SqlDataReader drCountry = null;
    >
    > try
    > {
    > conn.Open();
    > drCountry = countryLoad.ExecuteReader();
    >
    > countryDropLst.DataSource = drCountry;
    > countryDropLst.DataMember = "countryName";
    > countryDropLst.DataTextField = "countryName";
    > countryDropLst.DataValueField = "countryNo";
    > countryDropLst.DataBind();
    > }
    > finally
    > {
    > drCountry.Close();
    > conn.Close();
    > }
    > }
    >
    >
    >
    > But when I run it I get the error "The IListSource does not contain any
    > data sources", and the DataBind line is highlighted.
    >
    > I have checked the table using Enterprise Manager, it exists and has
    > numerous entries in both columns.
    >
    > Any idea what the problem might be?
    >
    > Thanks.
    >
    >
     
    =?Utf-8?B?bG9uZG9uIGNhbGxpbmc=?=, Jul 8, 2005
    #2
    1. Advertising

  3. RE: DataBind to DropDownList Problem "The IListSource does not con

    sorry ignore last post (i keep doing that) don't set the datamember

    "london calling" wrote:

    > you set your datareader to null... should be countryLoad.ExecuteReader()
    >
    > "Assimalyst" wrote:
    >
    > > Hi,
    > >
    > > I'm attempting to use a data reader to load data from a single table,
    > > tblCountry, with two columns, countryNo (the Key) and countryName, into
    > > a DropDownList box. Here's the code:
    > >
    > >
    > > protected System.Data.SqlClient.SqlConnection conn;
    > > protected System.Data.SqlClient.SqlDataReader drCountry;
    > >
    > > private void Page_Load(object sender, System.EventArgs e)
    > > {
    > > string SQL = "SELECT * FROM tblCountry";
    > >
    > > SqlConnection conn = new
    > > SqlConnection("Server=(local);Database=YLCdbSQL;Integrated
    > > Security=SSPI");
    > > SqlCommand countryLoad = new SqlCommand (SQL, conn);
    > > SqlDataReader drCountry = null;
    > >
    > > try
    > > {
    > > conn.Open();
    > > drCountry = countryLoad.ExecuteReader();
    > >
    > > countryDropLst.DataSource = drCountry;
    > > countryDropLst.DataMember = "countryName";
    > > countryDropLst.DataTextField = "countryName";
    > > countryDropLst.DataValueField = "countryNo";
    > > countryDropLst.DataBind();
    > > }
    > > finally
    > > {
    > > drCountry.Close();
    > > conn.Close();
    > > }
    > > }
    > >
    > >
    > >
    > > But when I run it I get the error "The IListSource does not contain any
    > > data sources", and the DataBind line is highlighted.
    > >
    > > I have checked the table using Enterprise Manager, it exists and has
    > > numerous entries in both columns.
    > >
    > > Any idea what the problem might be?
    > >
    > > Thanks.
    > >
    > >
     
    =?Utf-8?B?bG9uZG9uIGNhbGxpbmc=?=, Jul 8, 2005
    #3
  4. Assimalyst

    Assimalyst Guest

    Re: DataBind to DropDownList Problem "The IListSource does not con

    Thanks for the ideas,

    Removed the datamember, made no difference. Still getting the same
    error.

    Any more?
     
    Assimalyst, Jul 8, 2005
    #4
  5. Re: DataBind to DropDownList Problem "The IListSource does not con

    if you take the try catch block out does it throw an error?

    "Assimalyst" wrote:

    > Thanks for the ideas,
    >
    > Removed the datamember, made no difference. Still getting the same
    > error.
    >
    > Any more?
    >
    >
     
    =?Utf-8?B?bG9uZG9uIGNhbGxpbmc=?=, Jul 8, 2005
    #5
  6. Assimalyst

    Assimalyst Guest

    Re: DataBind to DropDownList Problem "The IListSource does not con

    Yep, same error.
     
    Assimalyst, Jul 8, 2005
    #6
  7. Re: DataBind to DropDownList Problem "The IListSource does not con

    > protected System.Data.SqlClient.SqlConnection conn;
    > protected System.Data.SqlClient.SqlDataReader drCountry;
    >
    > private void Page_Load(object sender, System.EventArgs e)


    Be sure to add the following

    if (!this.IsPostBack)
    {


    > {
    > string SQL = "SELECT * FROM tblCountry";
    >
    > SqlConnection conn = new
    > SqlConnection("Server=(local);Database=YLCdbSQL;Integrated
    > Security=SSPI");
    > SqlCommand countryLoad = new SqlCommand (SQL, conn);
    >
    >
    > try
    > {
    > conn.Open();
    > drCountry = countryLoad.ExecuteReader();

    //Try the following:
    while(drCountry.Read())
    {
    ListItem newListItem = new ListItem();

    newListItem.Value = drCountry.GetValue(0).ToString();
    newListItem.Text = drCountry.GetValue(2).ToString();
    // Add Items to DropDownAwardType DropDownList Web Control
    countryDropLst.Items.Add(newListItem);
    }


    > //countryDropLst.DataSource = drCountry;
    > //countryDropLst.DataMember = "countryName";
    > //countryDropLst.DataTextField = "countryName";
    > //countryDropLst.DataValueField = "countryNo";
    > //countryDropLst.DataBind();
    > }
    > finally
    > {
    > drCountry.Close();
    > conn.Close();
    > }
    > }


    }
     
    =?Utf-8?B?TW9vampvbw==?=, Jul 8, 2005
    #7
  8. Assimalyst

    Assimalyst Guest

    Re: DataBind to DropDownList Problem "The IListSource does not con

    Moojjoo, thankyou so much. I have been struggling with this for far too
    long now, at last it works!! :)
     
    Assimalyst, Jul 8, 2005
    #8
  9. Re: DataBind to DropDownList Problem "The IListSource does not con

    Hi Assimalyst, I have just noticed that you have been given a solution
    creating ListItems manually, if you still want to use databinding you can try
    this,
    HTH jd


    ================= into aspx==================
    <body>
    <form id="Form1" method="post" runat="server">
    <asp:dropdownlist id="ddlCountry" runat="server"></asp:dropdownlist></form>
    </body>

    ================ end aspx ======================
    ================begin code behind=================
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient ;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

    namespace csharp
    {
    /// <summary>
    /// Summary description for WebForm1.
    /// </summary>
    public class WebForm1 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DropDownList ddlCountry;

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion


    private void Page_Load(object sender, System.EventArgs e)
    {
    // Put user code to initialize the page here

    if(!Page.IsPostBack )
    {
    bindDropDown();
    }
    }

    private void bindDropDown()
    {
    //
    // get our datareader
    //
    IDataReader dr = DataAccess.CountryDB.GetCountryReader();

    //
    // bind our drop down
    //
    ddlCountry.DataSource = dr;
    ddlCountry.DataTextField = "CountryName";
    ddlCountry.DataValueField = "CountryNo";
    ddlCountry.DataBind ();

    //
    // close our datareader - note this will also close the database
    connection
    // because we used ocmd.ExecuteReader(CommandBehavior.CloseConnection)
    // when we opened it
    //

    dr.Close();

    }

    }

    //
    // ideally move these classes into a different .cs file / assembly
    // so the functionality can be reused in different apps / pages
    //
    namespace DataAccess
    {


    //
    //class for accessing data relating to country
    //

    public class CountryDB
    {
    public static IDataReader GetCountryReader()
    {
    //should ideally use a stored procedure here
    SqlCommand ocmd = new SqlCommand
    ("Your Select Query Here",
    DBConnection.GetDBConnection());
    return ocmd.ExecuteReader(CommandBehavior.CloseConnection) ;
    }

    }

    //
    // helper class to return a database connection
    //

    public class DBConnection
    {

    public static SqlConnection GetDBConnection()
    {
    //
    //ideally use configuration or other more secure store for
    connectionstring
    //

    //SqlConnection oconn = new
    SqlConnection(ConfigurationSettings.AppSettings("DBConnectionString")) ;
    SqlConnection oconn = new SqlConnection("Your Connection String Here");
    oconn.Open();
    return oconn;
    }
    }
    }

    }
    =================end codebehind======================

    "london calling" wrote:

    > if you take the try catch block out does it throw an error?
    >
    > "Assimalyst" wrote:
    >
    > > Thanks for the ideas,
    > >
    > > Removed the datamember, made no difference. Still getting the same
    > > error.
    > >
    > > Any more?
    > >
    > >
     
    =?Utf-8?B?bG9uZG9uIGNhbGxpbmc=?=, Jul 8, 2005
    #9
  10. Re: DataBind to DropDownList Problem "The IListSource does not con

    No problem. Now if I could just get an answer to my problem... AGH!!!

    Getting values from a user control that was added to a datagrid. This is
    driving me batty...

    "Assimalyst" wrote:

    > Moojjoo, thankyou so much. I have been struggling with this for far too
    > long now, at last it works!! :)
    >
    >
     
    =?Utf-8?B?TW9vampvbw==?=, Jul 8, 2005
    #10
  11. Assimalyst

    Assimalyst Guest

    Re: DataBind to DropDownList Problem "The IListSource does not con

    Thank you too London Calling, you obviously put quite an effort into
    forming your reply, it's much appreciated.

    I have implemented your method too, just to see how it works, and i
    think i will use it as i will be needing to use the same method on a
    number of pages, which your answer allows easily. (no offense Moojjoo!)

    Thank you both again, without your suggestions i think i would have
    caused my self a serious injury by now banging my head against that
    brick wall!! :)
     
    Assimalyst, Jul 8, 2005
    #11
    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. troutbum
    Replies:
    1
    Views:
    4,662
    =?Utf-8?B?S2VuIENveCBbTWljcm9zb2Z0IE1WUF0=?=
    Oct 6, 2004
  2. Replies:
    2
    Views:
    6,913
  3. =?Utf-8?B?V2FsaWQ=?=

    The IListSource does not contain any data sources.

    =?Utf-8?B?V2FsaWQ=?=, Sep 13, 2007, in forum: ASP .Net
    Replies:
    10
    Views:
    1,175
  4. Michael
    Replies:
    0
    Views:
    233
    Michael
    Dec 29, 2003
  5. KK
    Replies:
    1
    Views:
    292
    Srimathi
    Jan 13, 2005
Loading...

Share This Page